![]() |
Matlab中的结构(我找不到合适的标题!)
在我的方案中,我有100个节点。每当其中的一个随机节点生成数据时。我希望将它们记录在以前创建的文件中。
我一直在使用开关案例样式来打开与节点关联的特定文件。但是,对于100个节点来说已经很笨拙了,我需要增加节点数。我一直在寻找一种基于节点打开文件的直接方式。我在这里发现了一些提示: [URL="https://stackoverflow.com/questions/2349519/what-do-i-have-to-add-at-the-beginning-of-this-loop."]Stackoverflow_a_year_ago[/URL] 但是我无法选择并打开特定文件,例如,如果随机节点为125,我将打开n125.txt文件。任何帮助表示赞赏。代码如下: number_of_nodes=100; for i=1:number_of_nodes rand_node=ceil(rand(1,1)*100); rand_output=ceil(rand(1,1)*10); switch(rand_node) case{1} f1=fopen('n1.txt', 'a+'); fprintf(f1, rand_output); fclose(f1); case{2} f2=fopen('n2.txt', 'a+'); fprintf(f2, rand_output); fclose(f2) ; end end 另外,尝试了%.......................................... [INDENT]名称= dir('myprog * .TXT'); Names.name; %返回所有文件名。 [/INDENT] [B]回答:[/B] 也许我误会了您的问题,但答案似乎很明显: fid=fopen(sprintf('n%d.txt',rand_node), 'a+'); fprintf(fid, rand_output); fclose(fid); [url=https://stackoverflow.com/questions/5319508]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 22:45。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.