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)
Afficher commentaires plus anciens
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
Réponse acceptée
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}
11 commentaires
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))
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Entering Commands dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!