MATLAB Answers

Reading in different files based on a array of strings

1 view (last 30 days)
Brian Clyde
Brian Clyde on 8 Apr 2020
Commented: Brian Clyde on 8 Apr 2020
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.

  0 Comments

Sign in to comment.

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

Show 1 older comment
Brian Clyde
Brian Clyde on 8 Apr 2020
How would I read it from a different folder? say I have my app saved in my folder "Personal Project", and within that folder I have a folder labeled "Contact" that has C1 and C2 in it. how would I read them from there?
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)

Sign in to answer this question.


Translated by