uigetfile and getting Matrix from .txt file
2 views (last 30 days)
Show older comments
Hey guys,
I got a .txt Data and in my GUI I insert this file with a buttonclick. What is missing is, that the values are being displayed or saved in a Matrix (and in the next step, that another .txt file add their values in new rows into the same Matrix.)
What I already got (and think it works :D)
function get_file_Callback(hObject, eventdata, handles)
[filename1, pathname,d] = uigetfile( ...
{'*.txt;',...
'.txt-Dateien (*.txt)';
'*.*', 'All Files (*.*)'}, ...
'Wähle weise');
fid = fopen(fullfile(pathname,filename1),'r');
Now comes my tricky part. Where I got my problem
fid_input = textscan(fid, '%f%s%s%f%f%s','HeaderLines',2);
When I try to read fid_input I get
fid_input =
Columns 1 through 5
[4x1 double] {4x1 cell} {4x1 cell} [4x1 double] [4x1 double]
Column 6
{4x1 cell}
So somehow I guess it is right (my test file is 4x6), but are the values really in that Matrix? How can I have a look at the real Matrix? Where did I go wrong?
Thanks for helping.
0 Comments
Answers (1)
Geoff Hayes
on 3 Jul 2014
Yes, your output is correct given the format string that you have provided to the textscan function. From http://www.mathworks.com/help/matlab/ref/textscan.html, C = textscan(fileID,formatSpec) reads data from an open text file into cell array, C.
The values from your text file are in that matrix, and you did not do anything wrong.
fid_input is the cell array, with each element corresponding to a column of data from your file. The cell arrays within fid_input correspond to the string columns (as given by %s in your format string).
To access the first column of data, just type
fid_input{1}
and that will return the 4x1 vector of doubles. To get the second column of string data, just type
fid_input{2}
See Also
Categories
Find more on Large Files and Big Data 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!