How can I create a for cicle for the command 'readmatrix'?
Show older comments
I am trying to create a file.m like this; but if i have 321 file.txt (380_anaerobiosi.txt, 381_anaerobiosi.txt, 382_anaerobiosi.txt,....., untill to 700_anaerobiosi.txt) how can I repeat this cicle for all files to avoid to rewrite 321 strings?
T = readmatrix('/Users/chiara/Desktop/trachea16file/380_anaerobiosi.txt');
T1 = readmatrix('/Users/chiara/Desktop/trachea16file/381_anaerobiosi.txt');
T2 = readmatrix('/Users/chiara/Desktop/trachea16file/382_anaerobiosi.txt');
A = [T(20:30,2:2)];
B = [T1(20:30,2:2)];
C = [T2(20:30,2:2)];
X=[A,B,C]
csvwrite('X.csv',X)
2 Comments
madhan ravi
on 10 Jun 2020
P.S: KSSV approach is wrong, that would read all the text files from that folder.
Commenting here since he deleted my comment, without any notice.
@Madhan my intention is to show him to read all the files....so that he can try. It is not fair to comment without knowing what is my intention. Earlier also many times you have made such comments. Let the user try what he wants, let him learn and do what he wants. I am not here to provide the complete answer.
Commenting here since he deleted my comment, without any notice.
What notice you expect? Are you noticing me and making such comments?
Accepted Answer
More Answers (1)
txtFiles = dir('*.txt') ;
N = length(txtFiles) ;
iwant = cell(N,1) ;
for i = 1:N
thisFile = txtFiles(i).name ;
T{i} = readmatrix(thisFile) ;
end
1 Comment
Nimet KARAGÖZ
on 11 Jan 2021
I am trying something like that for app designer. But in my case user selects files so we cannot know how many files the user has and what are file names. When I tried to select one file it works. But when I try "MultiSelect" option with for loop it doesn't work. I get that error:
"Dot indexing is not supported for variables of this type."
When I delete ".name" I get that error:
"Error using readmatrix "filename" must be a string scalar or character vector."
How can I fix it?
% Button pushed function: UploadButton
function UploadButtonPushed(app, event)
[file,path] = uigetfile('*.txt');
selectedfile = fullfile(path,file);
sampleData_ref=readmatrix(selectedfile);
This is what I can achived. And below what I can't.
% Button pushed function: UploadButton_2
function UploadButton_2Pushed(app, event)
[file,path] = uigetfile('*.txt',...
'Select One or More Files',...
'MultiSelect', 'on');
remainFiles = fullfile(path,file);
files = length(remainFiles);
app.EditField.Value = files;
for run = 1: files
% switch run
% case {1,files}
thisFile = remainFiles(run).name;
sampleData_akt{run} = readmatrix(thisFile);
% end
end
end
Categories
Find more on Data Import and Analysis 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!