Reading in different files based on a array of strings

6 views (last 30 days)
I'm trying to create a app with the app designer program that lets the user obtain info from different files depending on some inputs given. I am having trouble getting the files to read in, as i want to list each file name as part of an array and then be able to choose which filename to read. Here's an example of test code I'm using.
Any ideas on how to do this?
Contact = ['C1.txt','C2.txt']; %this is the array of strings that i am using for the names of each file. It will be longer than 2 files
text = fileread(Contact(1)) %this gives me the output I want if I put the filename directly in here, but not if I try to read it from the array.

Accepted Answer

darova
darova on 8 Apr 2020
Use this simple construction
Contact = {'C1.txt','C2.txt'}; %this is the array of strings that i am using for the names of each file. It will be longer than 2 files
for i = 1:length(Contact)
text = fileread(Contact{i}); %this gives me the output I want if I put the filename directly in here, but not if I try to read it from the array.
end
  4 Comments
darova
darova on 8 Apr 2020
Try this
Contact = {'C1.txt','C2.txt'}; %this is the array of strings that i am using for the names of each file. It will be longer than 2 files
for i = 1:length(Contact)
fname = ['Contact\' Contact{i}];
text = fileread(fname); %this gives me the output I want if I put the filename directly in here, but not if I try to read it from the array.
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!