0
votes

Hello I am trying to create a GUI in Matlab. Using a push button I try to select .txt file an load it as a matrix. My only problem is that I can select the .txt file but I cannot load it in the workspace. Here what I have done till now:

function pushbutton1_Callback(hObject, eventdata, handles)
[filename, pathname] = uigetfile('*.txt', 'Select a MATLAB code file');
if isequal(filename,0)
   disp('User selected Cancel')
else
   disp(['User selected ', fullfile(pathname, filename)])
end

fileID = fopen(fullfile(pathname, filename)); % Open the file

A = fread(fileID); % Read from the file

fclose(fileID); % Close the file

S = char(A)

2
disp won't load data anywhere, let alone in GUI. Whatelse have you tried?Divakar
Yes I know that. I have tried the load command(filename) but did not work.Mikonio
Edit you question with that and show us the error/problem that might be popping up. First try to make it work in a script without GUI and once it does, you may post that code too.Divakar
I run the code to a .txt with values [1 2 3 4 5 6] and returns an A.mat having these values [49 13 10 50 13 10 51 13 10 52 13 10 53 13 10 54]. Just do not know what is wrong.Mikonio
Anyone can help me with that?Mikonio

2 Answers

1
votes

Once you have the filename you can try opening it and reading from it using the functions:

fileID = fopen(fullfile(pathname, filename)); % Open the file
A = fread(fileID); % Read from the file
fclose(fileID); % Close the file

Unfortunately how to best parse the data from the *.txt file to a matrix depends on the file as well as your specific needs.

0
votes

To load data into MATLAB from an ASCII (text) file you should use the importdata command. See here: importdata

Depending on whether you have delimiters (e.g., tab, comma) or hear lines in the text file, you would have to specify different input arguments to the command.