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

조회 수: 12 (최근 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
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).
Wiqas Ahmad
Wiqas Ahmad 2021년 3월 17일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Bjorn Gustavsson
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개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by