How to load .txt files into MATLAB from server?

3 views (last 30 days)
Hello All, I am currently having trouble geting the data in my .txt files to be displayed in MATLAB. I can see file names, but recieve errors when i run the full code. The maindir is just three random .txt files that have one line of text which says "test" and are saved in a folder on a server. All 3 files have different names(ex. test2.txt numerouno.txt,test3.txt) Any thoughts or suggestions would be greatly appreciated.
maindir=['U:\Random\text files for matlab code test'];
cd(maindir)
textfiles=dir('*.txt');
textfiles.name
numfiles=1:length(textfiles);
mydata=cell(1,numfiles);
for k=1:numfiles
mydata(k)=importdata(textfiles(k).name);
end
data=mydata(k);
disp(mydata)

Answers (1)

Geoff Hayes
Geoff Hayes on 14 Feb 2020
Austin - what is the error that you are observing? Is it
Error using cell
Size inputs must be scalar.
Note the code
numfiles=1:length(textfiles);
mydata=cell(1,numfiles);
where numfiles is an array with elements 1 2 3 ... up to the number of text files. I think that you want to do
numfiles=length(textfiles);
mydata=cell(1,numfiles);
instead. Also, since mydata is a cell array, you may want to try indexing with {} braces instead of () brackets
mydata{k}=importdata(textfiles(k).name);
  2 Comments
Austin Jandreau
Austin Jandreau on 18 Feb 2020
Hello Geoff Hayes,
First up thank you for responding to my question, it is myuch appreciated.
Secondly, Yes the error ithat was seeing was the following:
Error using cell
Size inputs must be scalar.
I tried out the recommendations and recieved a new error. The new error being seen is:
??? Error using ==> text
Invalid parameter/value pair arguments
Im going to play around with the code for a bit and try a few changes to see if i can get it to work, any thoughts on the new error?
Geoff Hayes
Geoff Hayes on 18 Feb 2020
Austin - I'd have to see the line of code that is generating the error message. Are you now calling the text function? Or doing something else?

Sign in to comment.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Products


Release

R2010a

Community Treasure Hunt

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

Start Hunting!