How to apply the loop index on the output (filename) of the sprintf function?
11 次查看(过去 30 天)
显示 更早的评论
The program gives the following output variables in the workspace as shown in figure. I have three questions regarding the output. 1) The loop index runs from n=4:1:6; but the output value of n is only 6. How can I convert n as a array matrix i.e [4 5 6]?. 2) The value of the filename shows only depout_6.mat perhaps n runs three times 4,5,6. I want the output filename according to the sequence of index n, i.e depout_4.mat, depout_5.mat, depout_6.mat. 3) The command window displays the results, dep_rat three times with increasing rows as shown in the second figure. I just want the last one to display with three rows. How can I resolve these issues?
clc;
clear all;
%------------------------------Program-------------------------------------
load EC.mat
for n =4:1:6;
filename = sprintf('depout_%d.mat',n);
data_out=load(filename,'dep');%dep is the variable in the filename(oute- FOV data in this case)
dep_out(n-3,:)=data_out.dep(n,:);
filename2 = sprintf('depinn_%d.mat',n);
data_inn=load(filename2,'dep');%dep is the variable in the filename(inner-FOV data in this case)
dep_in(n-3,:)=data_inn.dep(n,:);
dep_rat(n-3,:)=[dep_out(n-3,:)./dep_in(n-3,:)]% this is the depolarisaation_rat parameter in the paper. the value,...
%is greater than one because we calculated dep_rat=dep_out/dep_in. Calculating dep_rat=dep_in/dep_out will give you
%value less than one
end
4 个评论
Rik
2021-3-17
You can make n a vector, but that is probably not what you want if you want to use a for loop.
Why do you want to see those file names as well? Your code handles them inside the loop already?
I'm starting to suspect that this is not your code. If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks).
采纳的回答
Bjorn Gustavsson
2021-3-17
In your "workspace" panel you see the values (sizes) of the variables as they are after completing the loop. Your filname-variables have been depout_4.mat etc in previous iterations through the loop. You should be able to confirm this if you change the loop to this:
% clc;
% clear all;
%------------------------------Program-------------------------------------
load EC.mat
for n =4:1:6;
filename = sprintf('depout_%d.mat',n);
fprintf('n: %d, filename: %s\n',n,filename)
data_out=load(filename,'dep');%dep is the variable in the filename(oute- FOV data in this case)
dep_out(n-3,:)=data_out.dep(n,:);
filename2 = sprintf('depinn_%d.mat',n);
fprintf('n: %d, filename2: %s\n',n,filename2)
data_inn=load(filename2,'dep');%dep is the variable in the filename(inner-FOV data in this case)
dep_in(n-3,:)=data_inn.dep(n,:);
dep_rat(n-3,:)=[dep_out(n-3,:)./dep_in(n-3,:)]% this is the depolarisaation_rat parameter in the paper. the value,...
%is greater than one because we calculated dep_rat=dep_out/dep_in. Calculating dep_rat=dep_in/dep_out will give you
%value less than one
disp('push any key to continue (after looking at the workspace-panel...)')
pause
end
HTH
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!