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

14 vues (au cours des 30 derniers jours)
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 commentaires
Rebeca Lopez
Rebeca Lopez le 18 Fév 2021
@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?
Rebeca Lopez
Rebeca Lopez le 18 Fév 2021
@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.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 19 Fév 2021
Modifié(e) : Stephen23 le 19 Fév 2021
"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 commentaires
Stephen23
Stephen23 le 5 Mar 2021
Modifié(e) : Stephen23 le 5 Mar 2021
"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))
Rebeca Lopez
Rebeca Lopez le 8 Mar 2021
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!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Entering Commands dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by