Open txt file with UI control and pass to Class

5 views (last 30 days)
Good eve everybody,
Im trying to create an app to visualize data from a huge array. This array is stored in a CWF file but I can open it in notepad and read it easily so I gues I should be able to open it with a press on a button and start the OpenDialog select the file and pass the data to a class so I can extract all the data I need. Well it look easy but for some kind of reason I make a mistake and maybe someone can point out what I do wrong.
% So this code opens the dialog and I'm able to select the file I need. I
% expect that the file will be imported into A. Load data is a class where
% I want to exract everything.
function LoadChannel1ButtonPushed(app, event)
[file,path] = uigetfile('*.cwf');
if isequal(file,0)
disp('User selected Cancel');
else
A = importdata(file, ' ')
app.LoadData(A);
end
end
Unfortunately I get the error message 'unable to open file' , I have tried also the path together with the file but no luck. But when I code the load function with the path including the filename all goes well.
I think I make a mistake with file but I don;t see what.
Thanks for the help in advance

Answers (1)

Aditya
Aditya on 21 Jan 2025
Edited: Aditya on 21 Jan 2025
Hi Raymond,
The issue you're encountering is likely due to the fact that "importdata" requires the full path to the file, not just the filename. When you use "uigetfile", it returns both the filename and the path separately. You need to concatenate them to form the full file path before passing it to "importdata". Here's how you can modify your code:
function LoadChannel1ButtonPushed(app, event)
[file, path] = uigetfile('*.cwf');
if isequal(file, 0)
disp('User selected Cancel');
else
fullFilePath = fullfile(path, file); % Combine path and file
A = importdata(fullFilePath, ' '); % Use the full path
app.LoadData(A); % Pass the data to your class method
end
end
I hope this helps!

Community Treasure Hunt

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

Start Hunting!