Read in CSV files using GUI

If I want MATLAB to read in the following file using the GUIDE feature in MATLAB:
Source File:
ID:
C
C
C
C
R
F
L
T
E
Date Time Ch:
03/24/2012 28:43.0 -142.315
03/24/2012 28:43.0 -142.316
03/24/2012 28:43.0 -142.318
and so on,
how can I read the file if the extension is not .csv, say, .pl4?
With a .csv I could use the following but that only assumes if the file .csv which in my case it's not.
fid=fopen('filename.csv');
header = textscan(filenamecsv, '%s',3, 'delimeter' ',','headerLines',16);
data=textscan(filenamecsv, '%s %s %s', 'delimiter', ' ,');
data_to_plot=str2double(data{3});
fclose(filenamecsv);

 Accepted Answer

fid = fopen('filename.pl4');
header = textscan(fid, '%s', 3, 'headerLines', 16);
data = textscan(fid, '%s %s %f');
data_to_plot = data{3};
fclose(fid);

More Answers (1)

T
T on 13 Jan 2013
But if I create a function:
function openfile_Callback(hObject, eventdata, handles)
[filename,pathname,filterIndex] = ... uigetfile({'*.p4';),['Select the file'],... );
do I still need fid = fopen ?

14 Comments

Yes.
fullpath = fullfile(pathname, filename);
fid = fopen(fullpath);
T
T on 14 Jan 2013
Okay it works, however, when I check each line it appears that after fclose(fid)
[Y,M,D,H,MN,S] = datevec(data{2});
out = H*3600+MN*60+S;
doesn't give me the data in the file. Which is a bit odd. It outputed 2013 when I'm viewing a file from back in 2011.
Do you know what is wrong?
T
T on 14 Jan 2013
After fclose. I cannot plot my data
If you put a breakpoint in at the fclose() line, then can you see your data at that point? But then when you single-step the data is gone? Is the fclose() possibly giving an error that might be causing your function to exit, thereby destroying your function's workspace ?
Or is it perhaps not the fclose() that is the problem, and that the problem is that after the fclose() the function ends and you are trying to plot from a different function? If so then see
T
T on 14 Jan 2013
I managed to get it working. However, the plot that is created is embedded in the GUI which gets burried underneath the buttons. Is there a way for it to create a new window once plotted?
You can use figure() to create a new window, and figure() applied to an existing window raises that window to the top.
T
T on 15 Jan 2013
Edited: T on 15 Jan 2013
I managed to successfully plot after using fclose. However, I have another function, not using say handles.projectdata.data_to_plot
right after for which I can no longer recall that variable. Even if I use :
% Update handles structure
guidata(hObject, handles);
Do you know what I am talking about?
T
T on 15 Jan 2013
Edited: T on 16 Jan 2013
function openfile_Callback(hObject, eventdata, handles)
[filename,pathname,filterIndex] = ... uigetfile({'*.p4';),['Select the file'],... );
fullpath = fullfile(pathname, filename); fid = fopen(fullpath);
header = textscan(fid, '%s', 3, 'headerLines', 16);
data = textscan(fid, '%s %s %f');
data_to_plot = data{3};
fclose(fid);
using handles.project.data.(then the variable)
f = figure;
ax = axes('Parent',f);
plot(ax,handles.projectdata.time,handles.projectdata.data_to_plot);
grid(ax,'on')
You have not set handles.projectdata.time or handles.projectdata.data_to_plot in that code.
T
T on 16 Jan 2013
Okay. I got it working. Suppose I want the user to plot with this GUI, how can I have the user manipulate the plot by entering certain entries? Is there a reference you can provide? The documentation is long and I would be wasting time going through it all.
In particular, to have the user edit certain values for a variable. So say if x1 = data_to_plot
I want to be able to have the user to multiply x1 by any arbitrary number.
If you want to use a loading bar when opening files:
function openfile_Callback(hObject, eventdata, handles)
[filename,pathname,filterIndex] = ... uigetfile({'*.p4';),['Select the file'],... );
working = waitbar(0, 'Loading...');
close(working);
How do you get the bar to actually progress? it shows an empty bar but it doesn't show progress.
You cannot effectively use a waitbar in that situation, as you have no information about how long the load will take, and you are only doing a single operation (that is, a single textscan()).
T
T on 17 Jan 2013
Edited: T on 17 Jan 2013
suppose it takes 30 seconds? There is the for loop in the documentation for waitbar but it iterates then executes the commands I have after, delaying the processing rather than assessing it.
If you know how long it will take, use a timer object to update it, and hope that the innards of textscan() are not built-in functions (timers cannot interrupt built-in functions.)

Sign in to comment.

Categories

Find more on Environment and Settings 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!