Trying to load a .txt file in MatLab & save the data in workspace, all through GUI
2 views (last 30 days)
Show older comments
I'm creating a guide, where I will navigate to a folder, open a .txt file, and it will load the data into the workspace. The data will be an unknown amount of numbers, but in two columns. Later on in another GUI page, I will need to recall the data (hence needing it in the workspace) in order to cary out various math and graphing functions individually. So far this is what I have, but I'm hoping it's a simple answer to my question..
My original code -
global browse
[filename,filepath]=uigetfile({'*.*','All Files'},'select Data File 1');
cd(filepath);
browse = load(filename);
handles.browse = load([filepath filename]);
text = fileread(filename);
set(handles.batman, 'String', filename); %showing file name in box 1
set(handles.robin, 'String', text); %previews file in box 2
EDITS: I have tried inputting an fopen, fscan, and close in various ways put still get random errors with each one. Would setappdata/getappdata work anywhere?
I have tried putting the following between the handles.browse load and the sets, deleting the test=fileread.
handles.browse = fopen([filepath filename]);
data = fscanf(handles.browse,'%f',[12 Inf]);
fclose(handles.browse);
disp(data);
^^ This shows my data in two columns from the file, but does not load to workspace, and I get the following error -
Error using matlab.ui.control.UIControl/set
While setting the 'String' property of 'UIControl':
String should be char, numeric or cell array datatype.
0 Comments
Answers (1)
Walter Roberson
on 11 Apr 2016
global browse
[filename,filepath] = uigetfile({'*.*','All Files'},'select Data File 1');
browse = load(fullfile(filepath, filename));
Your global variable browse now contains the numeric contents of the file.
set(handles.robin, 'String', cellstr( num2str( browse ) ) )
The data will be converted to a formatted char array, that will be converted to a cell array of strings, and that will be sent to the uicontrol. The uicontrol should have been configured with Max greater than 1 in order to handle multiple lines.
0 Comments
See Also
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!