Mikonio
Mikonio

Reputation: 23

Load data and save data in Matlab GUI

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)

Upvotes: 0

Views: 2305

Answers (2)

Ryan J. Smith
Ryan J. Smith

Reputation: 1140

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.

Upvotes: 1

anon
anon

Reputation:

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.

Upvotes: 0

Related Questions