Clear Filters
Clear Filters

How do I pull data from a CSV into the GUI?

2 views (last 30 days)
I have a csv with a bunch of variables.
I want matlab to open the CSV, and put the first variable into one field, the second variable into the next field, etc.
How can I do this?
My csv file is actually a text file, test_file.txt Which contains: 1,120,5e-05,000035,5,1000000,2425,102e-05,1,2,3,4,1e-15,15,0,32
So I want Matlab to put 1, 120, etc into the various fields I have in the GUI.
Thank you for your help and time.

Accepted Answer

Bruno Pop-Stefanov
Bruno Pop-Stefanov on 20 Jan 2014
Edited: Bruno Pop-Stefanov on 21 Jan 2014
The following code should work:
% Open CSV file
fid = fopen('myCSV.txt', 'r');
% Read comma-separated values
A = fscanf(fid, '%f,');
% Don't forget to close the file
fclose(fid);
All your values will be stored in the vector A. You can then access them by calling A(1), A(2), A(3), etc...
Note that values have to be written in the text file like you said ("value1,value2,value3"); otherwise, it won't read them correctly. Let me know if you have more questions.
  3 Comments
Image Analyst
Image Analyst on 31 Jan 2014
Yes, put it in the callback for the submit button, if that is where you thing the best place to read in the file is. If you have a uitable and want to display the data in the grid/table control, then pass Bruno's "A" into set():
set(handles.uitable1, 'Data', A);
kayal gurunath
kayal gurunath on 3 Apr 2016
HI, I am reading a csv file and writing it to uitable on a button click. the data is getting overwritten on the same grid column for every line of csv file..how to get rid of the problem? am using the above mentioned code within the file reading loop

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!