How to plot a graph from a text file saved in a for loop?
1 次查看(过去 30 天)
显示 更早的评论
Hello everyone, I want to plot a graph from a text file which i save in my matlab code. The code goes something like this.
pause on
for i=1:50
% calculations %
.......
% saving a file %
name='output.txt';
fid = fopen(name,'a');
FORMAT = '%g ';
if i==1;
fprintf(fid, 'Date(m/d) Time(24hr) Carbon Money Grid Batt\n\n');
end
fprintf(fid,FORMAT,c_fc(2:3),c_fc(4),Carbon_saved(i),Money_saved(i), Grid(i), Batt(i));
fprintf(fid,'\n');
%%Plotting Graphs
if i<24
V=output(1:i,2);
U=output(1:i,5);
else
V=output(i-23:i,2);
U=output(i-23:i,5);
end
plot(V,U);
fclose(fid);
i=i+1;
pause(3600)
end
pause off
I want to draw a graph such that after every iteration it updates itself for last 24 values of the file. But this code gives error that "undefined variable output". Can someone help me in understanding how I can generate graph for the above condition. Also, I want to generate graph for the column 3 and 4 with respect to column 2. But, I think it will follow the same logic as before.
1 个评论
Adam
2015-4-20
It's not obvious why a file needs to be involved since you are writing it rather than reading it.
'output' is not defined because it is just the name you assigned to a text file.
If you use a matrix variable, output , instead and remove all use of file it would be a lot simpler.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!