Matlab: Text files, Read header lines and Cell references

Hi,
I am trying to cell reference header lines from the code below. I was a able to read header information line by line and printed tLine into command window. my problem is that when i cell referenced tLine(6) in the command window even though the tLine has been outputted to command window.
I got error message: >> tLine{6} Cell contents reference from a non-cell array object.
any help on how i can fix this error message?
Thanks,

4 Comments

This is a follow up of your previous question that you deleted : Your previous question
Why have you erased the question and the answers ?
@Matlab User: We are not your paid answering service, we are volunteers who come here to help other people: this does not mean just YOU, but actually everyone who might read this forum and find your question and our answers useful. When you delete your questions then you make our (volunteer) work useless for anyone else, and you are making it clear that you do not want our time to be useful to anyone else apart from yourself.
Deeply sorry, I value your time and dedication here.
@Stephen Cobeldick, this user is obviously new to MATLAB Answers. Maybe we shouldn't yell at them and make them afraid to ask for help.

Sign in to comment.

 Accepted Answer

The problem is that you're trying to referencing tLine as a cell array, but it's a string since you use fgetl, which returns a string:
function tline = fgetl(fid)
%FGETL Read line from file, discard newline character.
% TLINE = FGETL(FID) returns the next line of a file associated with file
% identifier FID as a MATLAB string.
When you get C from textscan, you get your cell array:
%function [varargout] = textscan(varargin)
%TEXTSCAN Read formatted data from text file or string.
% C = TEXTSCAN(FID,'FORMAT') reads data from an open text file identified
% by FID into cell array C.

11 Comments

Thank you for replying, Ben. how can I store tLine as cell array to read the header lines by referencing then?
I'm still not exactly sure what you want. Can you paste an example of your desired output? You can convert tLine into a cell array of strings by using strsplit:
display(tLine)
tLine =
25 data Col1 Col2 col3 col4
class(tLine)
ans =
char
display(strsplit(tLine))
'25' 'data' 'Col1' 'Col2' 'col3' 'col4'
class(strsplit(tLine))
ans =
cell
I want to cell reference the header lines so that can display them into matlab gui TEXT BOX.all the headerlines have been displayed into command window as tline= ***letter, tline=Date and soon.
What about my solution does not work for you? It converts tLine from a string to a cell array, which you can then reference as a cell array (i.e. from the above example, tLine{1} would produce '25').
unfortunately it is not working for my case.
fid= fopen([FilesToRead, MultipleFiles]);
Block=0;
while true
tLine = fgetl(fid);
if ~ischar(tLine)
break;
else
headerCells = strsplit(tLine,' ');
if length(headerCells) > 1
if ~isempty(headerCells{2})
if ~strcmpi(headerCells{2},'!user') && ~strcmpi(headerCells{2},'data')
textForGUI = headerCells{2}
end
end
end
end
if ~isempty(strfind(tLine,'data'))
Block=Block+1;
fprintf('\n\nBlock: %s\n\n', num2str(Block));
formatSpec = '%f %f %f %f %f';
C = textscan(fid,formatSpec,24,'CommentStyle','data','Delimiter','\t');
%here I got some calculations and plots
end
end
Thanks, Ben!
okay, this will be the end of my help because you're continuing to add on more and more conditions with every comment. this started out as a question of how to reference cell arrays, not parsing your whole text file and putting values into a GUI which i don't have any example code from.
anyway, this will put all the "headers" that you desire into a cell array 'textForGUI' and will still get you your cell array 'C' with the data in it. best of luck modifying it to suit your needs, and please mark my answer as correct since i did more than solve your original question :)
fid= fopen([FilesToRead, MultipleFiles]);
textForGUI = cell(1);
while true
tLine = fgetl(fid);
headerCells = strsplit(tLine,' ');
if length(headerCells) > 1
if ~isempty(headerCells{2})
if ~strcmpi(headerCells{2},'!user') && ~strcmpi(headerCells{2},'data')
textForGUI(end+1) = headerCells(2);
else
break
end
end
end
end
textForGUI = textForGUI(2:end);
Block=0;
while true
tLine = fgetl(fid);
if ~ischar(tLine)
break;
end
if ~isempty(strfind(tLine,'data'))
Block=Block+1;
fprintf('\n\nBlock: %s\n\n', num2str(Block));
formatSpec = '%f %f %f %f %f';
C = textscan(fid,formatSpec,24,'CommentStyle','data','Delimiter','\t');
%here I got some calculations and plots
end
end
ok. I will get back and accept it once I debug the problem and it's really appreciated. You actually provided a lot of help. But, now when I run the code I am getting this error massage.
Error using strsplit (line 76)
First input must be a char row vector.
Error in Untitled (line 18) headerCells = strsplit(tLine,' ');
The error occurred in the first while loop...Do you know what the problem is?
while true
tLine = fgetl(fid);
headerCells = strsplit(tLine,' ');
Type dbstop if error into the command window. When it errors, it will go into the editor and you can see what the value of tLine is. It is clearly not a character as it should be.
@Matlab User, to solve your problem:
  • step 1: accept Benjamin's answer
  • step 2: start a new question about (and only about) your new problem. Make sure that you use the {} Code button to format the code in your question properly.

Sign in to comment.

More Answers (0)

Categories

Products

Asked:

on 16 Aug 2016

Edited:

on 31 Aug 2016

Community Treasure Hunt

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

Start Hunting!