Store variable for different files with different names using for loop
Show older comments
Hello,
I need to read multiple text files from a directory and then process them as per their names and store each processed variable with different name for further processing.
Please find the code below and suggest me a way to store the varibales with different names using for loop or any other loop and also process them.
clc
clear all
close all
F = dir('*.txt');
nF = length(F) ;
count = 0 ;
for k=1:nF
prompt='Enter the value of Current (A) for which the data is plotted = ';
I=input(prompt);
sitename= num2str(I);
A=100;
j=I/A;
for ii = 1:nF
fname = F(ii).name ;
if contains(fname,sitename)
filename = fullfile(fname);
end
end
fileID = fopen(filename);
formatSpec = '%s';
N = 1;
c_text = textscan(fileID,formatSpec,N,'Delimiter',' ');
c_data= textscan(fileID,'%f %f %f %f %f');
if I==20
I20=c_data{1};
v20=c_data{2};
else if I==40
I40=c_data{1};
v40=c_data{2};
else if I==80
I80=c_data{1};
v80=c_data{2};
else
I100=c_data{1};
v100=c_data{2};
end
end
end
end
i20=-I20/1000;
i40=-I40/1000;
i80=-(I80/1000)+20;
i100=-(I100/1000)+40;
mi20=mean(i20)/A;
mv20=mean(v20);
mi40=mean(i40)/A;
mv40=mean(v40);
mi80=mean(i80)/A;
mv80=mean(v80);
mi100=mean(i100)/A;
mv100=mean(v100);
Thank you
2 Comments
"I need to read multiple text files from a directory and then process them as per their names and store each processed variable with different name for further processing."
No, you do not "need" to do that. Doing so would be a total waste of MATLAB's most powerful and useful feature: the ability to work with arrays. Putting numbering into variable names indicates that there is something wrong with the code design.
Accessing variable namess dynamically is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
You need to learn how to use indexing. Indexing is simple, neat, easy to debug, and very effiicent (unlike what you are trying to do). You should use indexing.
Bob Thompson
on 7 May 2019
Edited: Bob Thompson
on 7 May 2019
Please find the code below and suggest me a way to store the varibales with different names using for loop or any other loop and also process them.
You already have all of the information you want about the files in the F variable. As per Stephen's comment, it is completely unnecessary to make unique variables for each of the different entries, and is generally unwieldy to use dynamically named variables as a for loop entry. Just index your way through F.
Quickly looking over the code provided, it seems to already be doing what you want. If that code does not provide the results you are looking for, please provide greater detail on what the code is producing, and how it differs from what you are looking for.
Answers (1)
Anantrao Vijay Shirsath
on 10 May 2019
0 votes
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!