How to apply the loop index on the output (filename) of the sprintf function?

30 views (last 30 days)
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 Comments
Wiqas Ahmad
Wiqas Ahmad on 17 Mar 2021
Actuall the first row in the output dep_rat is related to 4, then 5 and the last is 6. I want to justify my output with showing these files.

Sign in to comment.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 17 Mar 2021
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

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!