Minimalist
Minimalist

Reputation: 975

Load File in GUI GUIDE to Read 2 Columns in MATLAB

I've been at this for 3 hours -- so I need help.

I have a button on MATLAB's GUI GUIDE to load a text file to store 2 columns of data as x and y.

So x = [12, 12, 23];

textfile A is:

  12 23
  12 32
  23 32

The code that is in the GUI GUIDE is under the pushbutton load_file as follows:

filename = uigetfile('*.txt')
loaddata = fullfile(pathname,filename)
load(loaddata)
A = filename(:,1)
B = filename(:,2)
handles.input1 = A;
handles.input2 = B;
axes(handles.axes1)
plot(handles.input1,handles,imput2)

Upvotes: 1

Views: 2316

Answers (2)

Keegan Keplinger
Keegan Keplinger

Reputation: 627

Firstly, you might want to post your error message to make sure I'm reporting on the right problem, but I can see one problem right off:

the lines:

A = filename(:,1)
B = filename(:,2)

are only retrieving a string naming the file, not the actual data. So first, you have to know the name of the data that is being loaded, then change the load line to:

data = load(loaddata,'-ascii')

and now:

A = data(:,1)
B = data(:,2)

Upvotes: 1

Jonas
Jonas

Reputation: 74940

load will load a text file, but it won't assign the contents to anything unless you explicitly specify an output.

%# load xy data from file
xy = load(loaddata,'-ascii')
%# assign columns to A and B, respectively
%# (why not x,y)?
A = xy(:,1)
B = xy(:,2)

The -ascii option of load is not necessary, but guarantees that the file is loaded as text, and will help you remember later that the data is supposed to be a text file.

Upvotes: 1

Related Questions