Why do I get "0x301 empty double matrix" when reading a cell array with data in it?

Hi, I have a text file that is converted to a cell array, so when I want to see if it is reading it properly, I show the cell array but it says "0x301 empty double matrix". As if it was empty, but it is not empty, it does have some data. Here's what I'm doing:
fid = fopen([pwd '\' char(filen)]); %here it selects the file I've stated from the current directory that I've also stated before
dat = textscan(fid, repmat('%f',1,301),'HeaderLines',2,'CollectOutput',1); % 301 columns to read, 2 header lines to skip
fclose(fid);
dat = dat{:}; % get the data in array format from cell
dat
Thanks!

4 Comments

that's hard to reproduce, your code seems fine.
can you share the file you want to read?
Haven't you forgotten delimiter in textscan format?
@Just Manuel thanks, sure here I upload the file. The original one has more rows but I had to sorten it so thatI could upload it here. Do you spot something wrong?
@Ive J thanks. I tried just now something like this:
dat = textscan(fid, repmat('%f',1,301),'HeaderLines',2,'CollectOutput',1,'Delimiter',',');
But it still shows as if it was empty. I receive the same message when I try to show a cell.

Sign in to comment.

 Accepted Answer

"Why do I get "0x301 empty double matrix" when reading a cell array with data in it?"
Because you specified two header lines, but the file actually has three header lines.
Here are the first few lines of the file (with added ellisions for brevity):
#1 ... lots of tabs here
"double data(36935,301)" ... more tabs here
#Space heating set-point temperature for day-zone in degrees Kelvin. ... more tabs
0 289.15 293.15 289.15 ... 289.15 289.15
600 289.15 293.15 289.15 ... 289.15 289.15
1200 289.15 293.15 289.15 ... 289.15 289.15
...etc.
Import the data by specifying three header lines:
rap = 'relative/absolute path to the folder where the file is saved';
fnm = fullfile(rap,'sh_day_short.txt');
fmt = repmat('%f',1,301);
opt = {'HeaderLines',3, 'CollectOutput',true};
[fid,msg] = fopen(fnm,'rt');
assert(fid>0,msg)
dat = textscan(fid, fmt, opt{:});
fclose(fid);
dat = dat{1}
dat = 996×301
0 289.15 293.15 289.15 293.15 289.15 291.65 289.15 294.65 289.15 289.15 289.15 289.15 289.15 289.15 293.15 289.15 289.15 289.15 289.15 294.65 289.15 289.15 289.15 293.15 289.15 294.65 293.15 289.15 294.65 600 289.15 293.15 289.15 293.15 289.15 291.65 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 293.15 289.15 289.15 289.15 289.15 294.65 289.15 289.15 289.15 293.15 289.15 289.15 289.15 289.15 294.65 1200 289.15 293.15 289.15 293.15 289.15 291.65 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 293.15 289.15 289.15 289.15 289.15 294.65 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 294.65 1800 289.15 289.15 289.15 293.15 289.15 291.65 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 293.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 294.65 2400 289.15 289.15 289.15 293.15 289.15 291.65 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 293.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 294.65 3000 289.15 289.15 289.15 293.15 289.15 291.65 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 293.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 294.65 3600 289.15 289.15 289.15 293.15 289.15 291.65 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 294.65 4200 289.15 289.15 289.15 289.15 289.15 291.65 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 294.65 4800 289.15 289.15 289.15 289.15 289.15 291.65 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 294.65 5400 289.15 289.15 289.15 289.15 289.15 291.65 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 289.15 294.65

11 Comments

Thanks @Stephen Cobeldick, ok I tried your solution but I don't really get the same as you. I realized that this short version of the text file has tabs separating each column, but in the original file it has commas instead.
And another difference is that the "second header line" is actually supposed to be without the quotation marks. So I attach a picture highlighting the differences between both text files.
If I keep the code as you say stating there are 3 headlines, the results jus say "NaN". But if I state there are 2 headlines, the result still shows "0×301 empty double matrix".
So I guess the problem is that I'm not stating the commas as delimiters as someone said before?
Thanks again!
"So I guess the problem is that I'm not stating the commas as delimiters as someone said before"
You uploaded a file which uses tab delimiters, and so that is what I used in my answer.
If your actual file uses some other delimiter, then of course you will need to specify that:
opt = {..other options.., 'Delimiter',','};
% ^^^^^^^^^^^^^^^ specify the delimiter!
Those quotation marks on the second line are unlikely to make any difference.
Yes perfect! I used:
opt = {'HeaderLines',2, 'CollectOutput',true,'Delimiter',','};
And it is indeed working now. Thank you so much for your help!
@Stephen Cobeldick, do you know why I'm not able to show a specific cell's value (like row 2, column 2) if I write for example:
value1=dat{2,2}
When I do so it shows this error: "Brace indexing is not supported for variables of this type"
Thanks!
If fmt contains only numeric specifiers and your code contains these two lines given in my answer.
dat = textscan(fid, fmt, opt{:});
...
dat = dat{1} % this extract the numeric array returned by TEXTSCAN.
then dat will be a numeric array. Curly brace indexing is only defined for the content of container array classes (e.g. structures, tables, strings, ..) and is not defined for numeric arrays (nor char, logical, ...). For a numeric array you would need to use parentheses:
val = dat(1,1)
% ^ ^ parentheses
How to use indexing with numeric arrays is explained in the MATLAB documentation:
I see. Ok thank you so much! I'll check also that information. I appreciate your help!
@Stephen Cobeldick, after reading that text file in Matlab and converting it to a numeric array, I applied a rule in order to change some fo the data. Do now I have my new numeric array which I would like to be overwritten in the text file. I'm using this code that I've already used before but it's not working.
fid = fopen([pwd '\' 'sh_day_short.txt'],'w');
fprintf(fid, '%s\n', '#1'); % I need this for another program to be used afterwards
fprintf(fid, '%s\n', ['double data(' num2str(size(dat,1)) ',' num2str(size(dat,2)) ')']); %also for the program
fclose(fid) ;
dlmwrite([pwd '\' 'sh_day_short.txt'],dat,'-append','delimiter' , '\t', 'precision',8)
I guess is because how we read the file according to your suggestion, but I don't know how to adjust it. Could you perhaps know what the problem is?
Thank you!
"I'm using this code that I've already used before but it's not working."
I don't know what "not working" means: does the file get written, but with incorrect data values? or the wrong format? Or is the file totally empty? Or does the code throw an error? Or does your computer catch on fire?
Please explain with a bit more information, so that I (and others) can help you.
Tip: to join filenames and filepaths together use fullfile rather than string concatenation.
Yes sorry, I it does not really rewrite anything on the file. The file is not modified at all.
Ok you mean to use fullfile like this?
myfile='sh_day_short.txt';
headline1 = fullfile('%s\n', '#1');
headline2 = fullfile['double data(' num2str(size(dat,1)) ',' num2str(size(dat,2)) ')'];
fid = fopen([pwd '\' myfile],'w');
fprintf(fid, headline1); % this is needed for Dymola input files
fprintf(fid, '%s\n', headline2); %also for Dymola
fclose(fid) ;
dlmwrite([pwd '\' 'sh_day_short.txt'],dat,'-append','delimiter' , '\t', 'precision',8) % here it's tab delimeted
"it does not really rewrite anything on the file. The file is not modified at all. "
Hmmm... curious. Are you sure that you are checking the right file? Double, triple, quadruple check this!
Use the file modification times (via DIR or as provided by your OS) to check if the file is being modified.
Try commenting-out the DLMWRITE call, perhaps it is clearing the file.
Try printing something a few times during that code, to make sure that it actually runs.
Add a breakpoint and step through line-by-line. If you open the file beforehand in a good file editor (e.g. Notepad++) it will alert you when the file has been changed and offer to reload the file data.
"you mean to use fullfile like this?"
No, just for concatenating filenames and filepaths. E.g. replace this:
[pwd '\' myfile]
with this:
fullfile(pwd,myfile)
Tip: replace this:
fprintf(fid, '%s\n', ['double data(' num2str(size(dat,1)) ',' num2str(size(dat,2)) ')'])
with
fprintf(fid, 'double data(%d,%d)\n', size(dat))
Ok I see. Yes, it is working now. I realized that it was storing it in a different location, similar to the one I was working on but I got confused and was not searching in the right place.
Thank you once again @Stephen Cobeldick for your valuable help, and sorry for bothering so much!

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!