Reading in different files based on a array of strings

2 vues (au cours des 30 derniers jours)
Brian Clyde
Brian Clyde le 8 Avr 2020
Commenté : Brian Clyde le 8 Avr 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.

Réponse acceptée

darova
darova le 8 Avr 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 commentaires
darova
darova le 8 Avr 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
Brian Clyde
Brian Clyde le 8 Avr 2020
Thank you! that worked!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by