3 views (last 30 days)
i have attached as much information as possible
so i have written a code to open a text file selected by user and to do whatever based on what the user chooses. this is the code that works.
[file, path] = uigetfile('.txt');
if isequal (file, 0)
disp ('user selected cancel')
disp(['user selected', fullfile(path,file)]);
COP = importdata(fullfile(path,file));
code works, no worries.
NOW the issue i am having is when the user wants to select and import multiple files. i have tried to do it using a for loop but it opens a structure and i dont know how to handle a struct. the loop i followed is similar to the one found in this link.. . so before anyone comes at me for not "trying" i have also tried stringcat and a lot of other methods, none of which worked.
its spits out this error
Error using importdata (line 139)
Unable to open file.
Error in 'trying' (line 13)
COP = importdata(fullfile(path,file));
COULD someone please provide some insight into why this is happening???? or what is wrong with my loop?
if you have nothing helpful to say please just don't say it. thanks.
sincerely a desperate student.

Accepted Answer

Simon Allosserie
Simon Allosserie on 26 Feb 2021
Edited: Simon Allosserie on 26 Feb 2021
The moment you select multiple files, file becomes a cell array.
Just run this in your command window and select a few files to see the data formats. Don't put ";" so you see how it looks.
[file, path] = uigetfile("Select something", 'MultiSelect', 'on')
You cannot make a fullfile of file, because it is no longer a string, but a cell array. Of course, Matlab cannot concatenate path (a string) with file (a cell array).
This means you will have to loop through your array and use something like
fullfile(path, file{1})
By the way, the answer you so nicely link in your question, contains the above information if you would read Jan's answer carefully.
Simon Allosserie
Simon Allosserie on 1 Mar 2021
Edited: Simon Allosserie on 1 Mar 2021
Hi Diala
If you wouldn't have so purposefully stated that in your question, I wouldn't have come back to it.
You should know that no one is born a matlabbing genius. We are all just trying to get better, just like you. We do this by interacting with this community and getting helped by many experts that provide us with answers we would ourselves never have thought of. There is no need to put yourself below others. I am posting questions myself regularly here, because sometimes I don't find the clue either.
There's just no need to shield yourself in your questions even before people are giving you answers. Every answer you get, requires someone's time and effort. Yes, I have also already received sassy responses. But they are still responses that most of the time will get you on the way, and help you form your thinking pattern.
So don't be hard on yourself. Also, don't be hard on others. If you don't find answers to your problems after researching yourself, always ask here. In the end, there will always be someone to help you. Over time, you will be one of the persons answering questions here. Think about that for a second. How nice will that be :)

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!

Translated by