I have a .M file from Octave that I want to run in MATLAB, but I obtain an error. Here is the content of .M file (it's just one line):
load "Slovenia_centered.mat"
And here is the error:
Error using load
Unable to read file '"Slovenia_centered.mat"': Invalid argument.
Error in
Slovenia_centered (line 1)
load "Slovenia_centered.mat"
^^^^

3 commentaires

Andiswa
Andiswa le 27 Fév 2026 à 5:06
Use single quote
dpb
dpb le 27 Fév 2026 à 12:55
Don't quote at all in command style
Stephen23
Stephen23 le 27 Fév 2026 à 13:45
Modifié(e) : Stephen23 le 27 Fév 2026 à 15:26
In command syntax single quotes are generally optional, but would be required if the filename (or other input) contained any space characters. This is explained in the documentation of command syntax as well as in the LOAD documentation which clearly states for command syntax "If any input includes spaces, enclose it in single quotes." Lets try it now, where we want to load data from a file named "test XXX" (the .mat is implicit):
XXX = 'hello world';
YYY = 'Wrong File!';
save test XXX YYY
YYY = 'Correct file!';
save 'test XXX' YYY
clearvars
For what it is worth, the LS command uses the same single quotes when required:
ls
test.mat 'test XXX.mat'
Now lets try to LOAD the data from the desired file "test XXX":
load test XXX YYY
YYY
YYY = 'Wrong File!'
load 'test XXX' YYY
YYY
YYY = 'Correct file!'

Connectez-vous pour commenter.

 Réponse acceptée

dpb
dpb le 13 Fév 2026 à 16:06
Modifié(e) : dpb le 13 Fév 2026 à 22:15
d=dir('Slovenia_centered.mat')
d = struct with fields:
name: 'Slovenia_centered.mat' folder: '/users/mss.system.hwgbc' date: '13-Feb-2026 16:09:15' bytes: 8972 isdir: 0 datenum: 7.4003e+05
type Slovenia_centered.mat
2.43585211e-01 4.34165188e-02 -1.12349916e-01 -2.28456591e-01 -1.91116589e-01 -2.70689717e-01 -3.11419208e-01 -3.50257617e-01 -6.65425832e-02 -9.14570313e-03 -8.20596965e-02 -1.78167486e-01 -2.97052722e-01 -2.46918797e-01 -2.19440188e-01 -2.70686443e-01 -3.18241940e-01 -2.10150356e-01 -2.13653827e-01 -2.17390732e-01 -2.62264223e-01 -1.49604529e-01 -1.09957459e-01 -9.49376074e-02 -1.24863682e-01 -3.15208845e-02 -8.90212675e-02 -3.55029042e-02 -8.28967479e-02 -1.76605387e-02 -2.30428121e-02 1.34293699e-03 -4.54977381e-02 6.45433851e-02 4.78045373e-02 7.24064017e-02 6.99667015e-03 9.52555827e-02 7.73363149e-02 8.51807013e-02 1.02811162e-02 1.13232188e-01 1.01257891e-01 8.56573993e-02 2.88423567e-03 9.61062203e-02 9.35117925e-02 8.53008908e-02 4.38889826e-04 1.25063364e-01 8.51262928e-02 7.02835862e-02 -1.06633213e-02 9.41939577e-02 7.82578294e-02 6.40646937e-02 2.02679656e-02 1.20137869e-01 1.24714671e-01 7.65958761e-02 3.86603250e-02 1.23444122e-01 1.21628133e-01 6.84510581e-02 -2.90423111e-02 4.92981441e-02 8.13086803e-02 4.57275391e-02 -4.60431416e-02 4.40207933e-02 7.70448326e-02 4.76952588e-02 -4.70176957e-02 5.09027753e-02 8.21718603e-02 5.11014038e-02 -3.14864492e-02 2.94709272e-02 5.25549402e-02 5.06473681e-02 -5.70459574e-02 2.85747947e-02 6.58264244e-02 8.00351937e-02 -1.53487772e-02 5.07730799e-02 8.77231592e-02 8.00775769e-02 -2.01877342e-03 7.10339434e-02 9.95038228e-02 8.51411552e-02 1.12622263e-02 7.48661734e-02 1.25845338e-01 1.13047672e-01 6.44208366e-03 8.20784658e-02 1.15861587e-01 1.16981801e-01 1.32878713e-02 8.13856501e-02 1.25175055e-01 1.33878725e-01 -1.98675542e-03 7.90652670e-02 1.35774710e-01 1.08919874e-01 -7.07711107e-02 8.92207774e-02 -4.65527936e-02 -1.57130551e-01 -2.40994223e-01 -4.96244659e-02 -1.52453018e-01 -2.12563368e-01 -2.67801452e-01 4.95756179e-02 -4.40686368e-02 -1.15580766e-01 -1.25775054e-01 -1.73384135e-01 -1.52179917e-01 -1.43851209e-01 -2.10410045e-01 -1.97070525e-01 -1.41407124e-01 -1.27659408e-01 -1.55943094e-01 -1.35964420e-01 -1.31516033e-01 -1.19209025e-01 -8.49613192e-02 -6.95622210e-02 4.68736421e-02 -1.26130498e-01 -6.93363832e-02 -5.69920136e-02 -6.40601175e-02 -3.47545081e-02 -2.75506476e-02 -3.69073157e-02 -2.91088444e-02 6.72125124e-03 -1.48267606e-02 -4.09041711e-02 -2.43471523e-02 -1.06314026e-02 2.90942493e-02 1.06398348e-02 5.28797535e-02 5.81501672e-02 6.46773605e-02 7.99001652e-02 5.88197546e-02 9.74155904e-02 6.80675013e-02 3.69971704e-02 7.15738061e-02 4.92185352e-02 8.51880605e-02 5.25690589e-02 4.70319887e-02 8.73093630e-02 1.24347841e-01 8.34957748e-02 8.01157151e-02 1.04087442e-01 5.52314058e-02 1.03144746e-01 8.71002361e-02 8.58075469e-02 -3.39072211e-02 -1.60503962e-01 -1.63662056e-01 -1.32633635e-01 -1.58251994e-01 -1.91495362e-01 -1.29823541e-01 -9.37579206e-02 -5.39783749e-02 -3.68312926e-02 -2.66630432e-02 3.32789371e-03 3.26337438e-02 1.01304451e-02 2.23581354e-02 5.22598442e-02 4.47007363e-02 1.19954623e-01 1.03389332e-01 1.06359929e-01 1.46719725e-01 1.30101143e-01 1.12428279e-01 1.30705523e-01 8.45389663e-02 1.08088593e-01 9.75684648e-02 1.01395119e-01 9.71937745e-02 3.47484905e-02 4.42750299e-02 8.25424161e-02 9.21584290e-02 1.13038218e-01 1.19697351e-01 1.14179812e-01 1.44723437e-01 1.28270457e-01 1.21420846e-01 1.45170111e-01 1.37681317e-01 1.21213960e-01 9.30495370e-02 7.33983930e-02 2.96409051e-02 8.40368523e-03 1.53894823e-01 1.16035111e-01 5.81934066e-02 1.08556223e-02 4.86781106e-02 -1.05331625e-02 -3.43596071e-02 -7.51130657e-02 -1.00049299e-01 -1.14882779e-01 -1.45116531e-01 -1.57761801e-01 -1.83074680e-01 -1.80367065e-01 -1.54923112e-01 -1.84838759e-01 -2.11886923e-01 -1.99723197e-01 -1.58956814e-01 -1.51969260e-01 -1.50441005e-01 -1.04679706e-01 -9.98488344e-02 -9.69258753e-02 -7.92101973e-02 -6.23525044e-02 -9.78704987e-02 -7.80931130e-02 -7.38248638e-02 -3.76943507e-02 -4.05711033e-02 -2.24749292e-02 -1.46918983e-02 3.47575728e-02 3.09042411e-02 6.48459274e-02 6.48847362e-02 6.39247697e-02 5.58703001e-02 8.41531535e-02 4.15551778e-02 3.08979935e-02 3.04199093e-02 1.50047061e-02 -1.43383975e-02 -2.64013521e-02 -1.13675746e-02 -1.78031382e-02 -2.24706655e-02 -1.23830497e-01 -1.52337276e-01 -1.02269500e-01 -7.09244756e-02 -5.14008250e-02 -2.04815280e-02 3.98515553e-03 -9.94963306e-03 5.81655913e-04 6.20384609e-02 3.66873561e-02 4.44709790e-02 3.01924135e-02 7.34990120e-02 1.01999638e-01 8.96974405e-02 8.91687653e-02 8.25398823e-02 5.87013493e-02 4.75817750e-02 3.78256882e-02 5.58710325e-02 3.69790976e-02 2.76071724e-02 2.99780538e-02 4.58801499e-02 4.09223762e-02 3.63845571e-02 3.68050568e-02 2.64511191e-02 2.18297644e-02 1.92599831e-02 9.93448050e-03 1.63902029e-02 1.54940734e-02 2.20701954e-02 2.61649928e-02 6.15279108e-02 5.35505614e-02 5.06709261e-02 4.76384299e-02 6.49500327e-02 5.76095342e-02 7.29864361e-02 5.68674368e-02 8.22899297e-02 7.86909945e-02 7.71742431e-02 7.29094161e-02 7.81446248e-02 6.99662839e-02 7.20843451e-02 6.40103939e-02 7.74977970e-02 7.01916143e-02 6.66090971e-02 5.74273222e-02 7.55973754e-02 5.82662735e-02 4.72813797e-02 2.64495060e+01 1.12540703e+01 1.20777772e+01 2.28580935e+01 1.38260053e+01 1.31007766e+01 1.10022506e+01 1.66790277e+01 8.22458464e+00 -2.49031719e-01 5.29807266e-01 5.65329456e+00 6.25817197e+00 8.02464904e+00 3.49623740e+00 5.45883295e-01 2.39698471e+00 3.14655001e+00 4.34955656e+00 4.77493419e+00 5.03013884e+00 4.89452079e+00 2.62649397e+00 9.42308053e-01 1.09752442e+00 -2.52475578e-01 9.60857755e-01 2.62752442e+00 3.61085776e+00 4.03085776e+00 3.93085776e+00 3.93085776e+00 3.70419109e+00 4.02419109e+00 3.91085776e+00 2.20752442e+00 2.62752442e+00 3.29085776e+00 3.45752442e+00 2.02419109e+00 2.40419109e+00 1.65419109e+00 1.29419109e+00 1.07085776e+00 6.34191089e-01 -4.39142245e-01 -1.13914224e+00 -1.10580891e+00 -1.12580891e+00 -1.12580891e+00 -1.14580891e+00 -1.16914224e+00 -1.38580891e+00 -1.71580891e+00 -1.70580891e+00 -1.57247558e+00 -1.35547558e+00 -1.11097558e+00 -6.75308911e-01 -4.51042245e-01 -6.95808911e-01 -3.15342245e-01 -1.94008911e-01 -9.61142245e-01 -3.16410891e+00 -3.86517558e+00 -4.30660891e+00 -4.45390891e+00 -4.51367558e+00 -4.48954224e+00 -4.30087558e+00 -4.15534224e+00 -4.08267558e+00 -3.76417558e+00 -3.61374224e+00 -3.68024224e+00 -4.13280891e+00 -4.47977558e+00 -4.81724224e+00 -4.98067558e+00 -4.96434224e+00 -4.96900891e+00 -4.95230891e+00 -4.93490891e+00 -4.88067558e+00 -4.87724224e+00 -5.01124224e+00 -5.09434224e+00 -5.12977558e+00 -5.18234224e+00 -5.20360891e+00 -5.26497558e+00 -5.36187558e+00 -5.43390891e+00 -5.47390891e+00 -5.48830891e+00 -5.50360891e+00 -5.50577558e+00 -5.50544224e+00 -5.50460891e+00 -5.50410891e+00 -5.50104224e+00 -5.49530891e+00 -5.49114224e+00 -5.48434224e+00 -5.49290891e+00 -5.57254224e+00 -5.57877558e+00 -5.58130891e+00 5.46880734e-02 2.66880734e-02 2.96880734e-02 4.76880734e-02 2.66880734e-02 3.46880734e-02 2.46880734e-02 3.36880734e-02 1.26880734e-02 -3.11926606e-04 8.68807339e-03 1.26880734e-02 1.96880734e-02 1.06880734e-02 -1.13119266e-02 9.68807339e-03 1.26880734e-02 1.66880734e-02 -3.11926606e-04 6.68807339e-03 1.76880734e-02 5.68807339e-03 -9.31192661e-03 -3.11926606e-04 4.68807339e-03 -3.31192661e-03 1.96880734e-02 6.68807339e-03 1.36880734e-02 4.68807339e-03 1.26880734e-02 5.68807339e-03 1.46880734e-02 1.16880734e-02 -1.31192661e-03 -6.31192661e-03 1.96880734e-02 4.68807339e-03 2.68807339e-03 -6.31192661e-03 1.26880734e-02 3.68807339e-03 -8.31192661e-03 -9.31192661e-03 6.88073394e-04 6.68807339e-03 -1.43119266e-02 -1.03119266e-02 6.88073394e-04 -8.31192661e-03 6.88073394e-04 -1.73119266e-02 -5.31192661e-03 2.68807339e-03 -5.31192661e-03 -1.23119266e-02 -7.31192661e-03 1.26880734e-02 -8.31192661e-03 7.68807339e-03 6.88073394e-04 1.56880734e-02 -1.93119266e-02 -2.83119266e-02 -1.31192661e-03 6.88073394e-04 -2.03119266e-02 -7.31192661e-03 -5.31192661e-03 4.68807339e-03 -2.13119266e-02 -7.31192661e-03 -2.31192661e-03 -4.31192661e-03 -1.43119266e-02 -7.31192661e-03 6.88073394e-04 -3.31192661e-03 -2.31192661e-03 -1.33119266e-02 -7.31192661e-03 -3.31192661e-03 -8.31192661e-03 -1.93119266e-02 -1.13119266e-02 6.88073394e-04 -2.03119266e-02 -1.93119266e-02 -1.33119266e-02 -5.31192661e-03 -2.03119266e-02 -1.43119266e-02 -1.83119266e-02 3.68807339e-03 -1.93119266e-02 -1.13119266e-02 -3.31192661e-03 -6.31192661e-03 -1.43119266e-02 -5.31192661e-03 -8.31192661e-03 1.68807339e-03 -1.53119266e-02 -1.33119266e-02 -7.31192661e-03 5.68807339e-03 -1.63119266e-02 -1.23119266e-02 -1.93119266e-02
Aha! It's text, not native .mat.
x=load('-ascii','Slovenia_centered.mat')
x = 545×1
0.2436 0.0434 -0.1123 -0.2285 -0.1911 -0.2707 -0.3114 -0.3503 -0.0665 -0.0091 -0.0821 -0.1782 -0.2971 -0.2469 -0.2194
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
To write data from Octave to be imported into MATLAB, it would be more portable to either explicitly write text data with fprintf or whatever other higher-level functions Octave has implemented in parallel with MATLAB or as native binary data with fwrite to read with fopen and fread
Although, presuming Octave has it implemented, if it can write a specific version of a .mat file that has the benefit of also storing the variables and shape and type information that alternate forms don't without extra effort. Just have to be sure to write in a known version and then verify it does work as expected (not that I have any reason to think it wouldn't; Octave has been around for a long time).

17 commentaires

Svit
Svit le 14 Fév 2026 à 16:42
Déplacé(e) : dpb le 14 Fév 2026 à 16:49
Thank you so much dpb. One further question. I need the data not in one column, but in five columns, such as in the attached file Slovenia. The data include observations of five variables: consumption, investment, interest rates, inflation rate and monetary aggregate M3. How could I split the data in multiple columns? I searched the questions on this forum, as well as on Google and AI (Copilot), but I haven't found anything useful.
My code looks like this, it includes temp variable that includes five types of data:
temp = [ data];
Dataslpiobs=temp(:,1);
Dataslinobs=temp(:,2);
Dataslcobs=temp(:,3);
Dataslrobs=temp(:,4);
Dataslmobs=temp(:,5);
Your help is much appreaciated!
dpb
dpb le 14 Fév 2026 à 17:35
Modifié(e) : dpb le 15 Fév 2026 à 15:30
I moved to continue the same thread -- dpb
That depends on how the data were written...let's see what the file actually contains...
%type Slovenia
Slovenia
whos
Name Size Bytes Class Attributes Dataslcobs 109x1 872 double Dataslinobs 109x1 872 double Dataslmobs 109x1 872 double Dataslpiobs 109x1 872 double Dataslrobs 109x1 872 double ans 1x34 68 char temp 109x5 4360 double
Well, we have an array and the others that were created from them. OK, now see what was mentioned in the comment...
Presuming you know a prior the variable names to use, then you can more directly convert to a table from the 2D array as
vnames={'Dataslcobs','Dataslinobs','Dataslmobs','Dataslpiobs','Dataslrobs'};
tData=array2table(temp,'VariableNames',vnames);
head(tData)
Dataslcobs Dataslinobs Dataslmobs Dataslpiobs Dataslrobs __________ ___________ __________ ___________ __________ 0.067 0.037379 0.18434 31.625 0.11012 0.039 -0.098394 -0.015834 16.43 0.072263 0.042 -0.20897 -0.1716 17.254 0.014422 0.06 -0.29284 -0.28771 28.034 -0.032916 0.039 -0.10147 -0.25037 19.002 0.0049062 0.047 -0.20429 -0.32994 18.277 -0.054305 0.037 -0.2644 -0.37067 16.178 -0.078131 0.046 -0.31964 -0.40951 21.855 -0.11888
However, here's no reason to make/have two copies of the same data; if you have a (again text) .mat file that is the variables as a single array, then you can read it into the table directly as
fn='OctaveArrayDataFile.mat'; % assume text file
tData=readtable(fn,'FileType','text','VariableNames',vnames);
where like with load have to tell MATLAB a .mat file is in text format rather than native form and it is again required to know the variable names to use (and in which order they are in the 2D array) from prior knowledge
You can then apply all the power of MATLAB to the table referencing each by name with whatever it is that is wanted to be done.
The underlying Q? is how are you getting these .mat files -- are you creating them yourself or is somebody providing them? Can you (or they) change how they're being written to save as -V7 MATLAB .mat files in which case you'll get the variable names and sizes automagically?
What specifically is to be done might aid in other more specific recommenations.
Svit
Svit le 15 Fév 2026 à 16:03
1. I think I can't use command fn='OctaveArrayDataFile.mat';, because I obtain error:
Error using readtable (line 19)
Ambiguous parameter name: VariableNames.
I assume I have to substitute name 'OctaveArrayDataFile.mat' with an actual name of my .MAT file, i.e. fn='Slovenia_centered.mat';? Even if I don't do the substitution, I obtain the same error code...perhaps the file isn't a text file?
2. I created the .MAT file from existing Slovenia.m file using Octave's commands for demeaning (deducting means) for all 5 variables: Dataslrobs=center(Dataslrobs) (repeating this five times for all 5 variables) and then "save -ascii Slovenia_centered.mat Dataslcobs Dataslinobs Dataslmobs Dataslrobs Dataslpiobs" in order to save the result. The original .M file was provided to me by users of Dynare forum.
3. The reason I need the temp variable, that contains the 5 variables, is that I need them for my Dynare's .MOD file, that includes references to 5 variables. I can't upload .MOD file here because the format isn't supported, so I converted it into a .TXT file, please find it attached.
Thank you.
dpb
dpb le 15 Fév 2026 à 16:53
Modifié(e) : dpb le 19 Fév 2026 à 16:14
"I think I can't use command fn='OctaveArrayDataFile.mat';,"
Yeah, I forgot that one can't name variables in the readtable function call.
" I need the temp variable, that contains the 5 variables,..."
But, there's a problem here in that the file is just one list of values -- how was the temp array constructed--by knowing a priori that the file is supposed to contain exactly five variables, I presume?
I had thought when you showed that m-file that the file had been arranged such that the temp array was being read as the 2D array and so the file itself had five columns. Unless there are other files, that isn't true and so the previous assumptions all are in error.
d=dir('S*cent*.mat'); % get a dir() for the file(s)
fn=d.name; % the name without having to type long name <vbg>
load('-ascii',fn)
whos
Name Size Bytes Class Attributes Slovenia_centered 545x1 4360 double ans 1x45 90 char d 1x1 960 struct fn 1x21 42 char
So, what is obtained after the load is, indeed, just one long vector so
try
assert(mod(Slovenia_centered,5)==0) % make sure is divisible by 5
catch
error('Vector length (%d) not compatible; unable reshape to 5 variables. Aborting.', ...
numel(Slovenia_centered))
end
Slovenia_centered=reshape(Slovenia_centered,[],5); % reshape to 2D presuming are 5 variables
whos
Name Size Bytes Class Attributes Slovenia_centered 109x5 4360 double ans 1x45 90 char d 1x1 960 struct fn 1x21 42 char
In this case, there actually are 109 rows of 5 elements each, not precisely 500 as the prior code produced. You'll have to decide which 9 are immaterial to throw away or keep them all; we have no way to know. NOTA BENE: the above just reshapes the same variable in place, doing away with the temp arrray and includes a check that the vector length is divisible by 5.
You already have the code to create separately named variables from the array given you know what names you want. With the 1D vector of just numeric values, there really isn't any more effective way to take the next step. One ugly coding way that is not recommended to "poof" variables into the workspace is eval, but as the following shows, that's ugly and very error prone and difficult to debug as well as being slow---but that said,
vnames={'Dataslcobs','Dataslinobs','Dataslmobs','Dataslpiobs','Dataslrobs'};
for i=1:numel(vnames)
cmd=sprintf('%s=Slovenia_centered(:,i);',vnames{i}); % make a command to execute
eval(cmd); % and evaluate the string
end
whos D* % see who we made
Name Size Bytes Class Attributes Dataslcobs 109x1 872 double Dataslinobs 109x1 872 double Dataslmobs 109x1 872 double Dataslpiobs 109x1 872 double Dataslrobs 109x1 872 double
return % just in here so online "run" won't error on next lines
As noted before, if you would use the option in Octave to write a compatible -V7 .mat file, then you could load exactly what you have in Octave into MATLAB as the five variables as those named variables in the one load command. But, as is, the variable names aren't in the file and it's just a 1D text vector.
% OCTAVE CODE snippet...
Dataslrobs=center(Dataslrobs);
...
save -V7 Slovenia_centered.mat Dataslcobs Dataslinobs Dataslmobs Dataslrobs Dataslpiobs
should do the trick presuming Octave really does implement the MATLAB -mat file correctly. Then just
% MATLAB code
load Slovenia_centered.mat
should give you back the five variables. I haven't had an install of Octave in 20 years so can't test locally, but it's supposed to work.
The other Q? then becomes, if you have access to MATLAB to read these files, why are you creating them with Octave instead of MATLAB to begin with? MATLAB can do everything Octave can do without the hassle. You might have to fix a few syntax errors if somebody used some of the Octave peculiarities that MATLAB doesn't, but it shouldn't be bad.
There's nothing preventing you from just using the existing script to read the vector and manually create the five specific variables from the array. Given the way the present Octave script is writing it as a 1D text array without headers, that's about the best that can be done if you aren't going to change the way Octave writes the data (or just forget Octave and do it in MATLAB to begin with).
Stephen23
Stephen23 le 15 Fév 2026 à 16:57
"I created the .MAT file from existing Slovenia.m file using Octave's commands for demeaning (deducting means) for all 5 variables: Dataslrobs=center(Dataslrobs) (repeating this five times for all 5 variables) and then "save -ascii Slovenia_centered.mat Dataslcobs Dataslinobs Dataslmobs Dataslrobs Dataslpiobs" in order to save the result"
Then why not save yourself all of this trouble by saving using the option -V7 ?
Converting your data to text and back again is a superfluous indirection. Use a binary file format!
Svit
Svit le 18 Fév 2026 à 15:52
The reason I produced these files in Octave is that I had no access to MATLAB before. I purchased it now and now I plan to use MATLAB only.
Thank you very much for the code, the first part works just fine, but the following part produces an error:
try
assert(mod(Slovenia_centered,5)==0) % make sure is divisible by 5
catch
error('Vector length (%d') not compatible; unable mae to 5 variables. Aborting.', ...
numel(Slovenia_centered,5))
end
Slovenia_centered=reshape(Slovenia_centered,[],5); % reshape to 2D presuming are 5 variables
whos
The error:
error('Vector length (%d') not compatible; unable mae to 5 variables. Aborting.', ...
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets
instead of parentheses.
Stephen23
Stephen23 le 18 Fév 2026 à 16:14
Modifié(e) : Stephen23 le 18 Fév 2026 à 16:15
If you require that single quote after the integer:
'Vector length (%d'') not compatible; unable mae to 5 variables. Aborting.'
% ^^
Otherwise:
'Vector length (%d) not compatible; unable mae to 5 variables. Aborting.'
dpb
dpb le 19 Fév 2026 à 16:18
Modifié(e) : dpb le 19 Fév 2026 à 21:06
Problem with trying to edit inline in the forum without syntax checking in place...changed horses in mid-stream about adding the message in the assert call to separate catch clause and didn't catch all the fixups needed including the dangling quote.
What I really intended was something more on the order of
error('Vector length (%d) not compatible; unable reshape to 5 variables. Aborting.', ...
numel(Slovenia_centered))
There was also a leftover ",5"" from the initial mod() operation pasted in plus a typo in the error message text itself.
"...now I plan to use MATLAB only."
You'll probably find that a good decision in the long run.
I'll note in doing so that earlier you mentioned "demeaning (deducting means) for all 5 variables: Dataslrobs=center(Dataslrobs) (repeating this five times for all 5 variables) ".. Note that in MATLAB (and Octave, too, for that matter) virtually all builtin functions operate by column so it isn't necessary to either loop or duplicate variable names in such a fashion; it's a general sign of not using the vector ability of MATLAB effectively. Instead, use arrays and if need to identify columns in the array, use the table or named fields in a structure; these can be easily manipulated programmatically with direct code to address them rather than having to use contorted features such as eval or duplicate code lines multiple times to operate on the data. Although MATLAB does not explicitly incorporate the center function, it uses
s_c=detrend(s,0); % remove best 0-order trend (mean) from array s by column
The above is identical to
s_c=s=mean(s);
and, of course, if the mean isn't of interest, one can always just replace the original data in place and if wanted, keep the means in another array.
m=mean(s); % compute means of s
s=s-m; % remove means, keeping their values if want later
Of course, one could add their own(*) center function via a user m-file or via an anonymous function...
center=@(a)a-mean(a); % anon function center
although one can't support the optional dim argument that way.
(*) As a personal use pattern, besides a default Work directory I create an Utilities folder in which I keep such generally useful functions that aren't (yet) built into MATLAB. I permanently add these two folers at the top of the MATLAB path so anything in Work, then Utilities is called first.
If I have a specific piece of work for a given client or job, I create its own folder and have a pattern for setting a work environment that will save the current default path and then move to the other environment incorporating any other specific pieces needed for it. These uniformly also rely on the Utilites being available. I can then use the optional input parameter with a value of 'quit' to exit that location and return to the base Work location. That has been most helpful working model to keep things localized but have a common toolset available regardless.
Svit
Svit le 20 Fév 2026 à 9:34
Modifié(e) : Svit le 20 Fév 2026 à 9:35
Thank you very much for the code, I will use the vector ability of MATLAB from now on!
But you said I have to make sure the number is divisible by 5. However the following code produces an error, although 545 is divisible by 5. Code:
try
assert(mod(Slovenia_centered,5)==0) % make sure is divisible by 5
catch
error('Vector length (%d) not compatible; unable reshape to 5 variables. Aborting.', ...
numel(Slovenia_centered))
end
Slovenia_centered=reshape(Slovenia_centered,[],5); % reshape to 2D presuming are 5 variables
whos
Error:
Vector length (545) not compatible; unable reshape to 5 variables. Aborting.
dpb
dpb le 20 Fév 2026 à 12:40
Modifié(e) : dpb le 20 Fév 2026 à 17:33
You need to double-check on me, too...in that code the numel() call to get the length is missing......
Slovenia_centered=randn(545,1);
assert(mod(numel(Slovenia_centered),5)==0)
disp('Done')
Done
Runs w/o throwing error...
Svit
Svit le 21 Fév 2026 à 9:26
Modifié(e) : dpb le 21 Fév 2026 à 13:01
I hope I will be as great MATLAB programmer as you one day :D.
Okay, so the following code now works perfectly:
%type Slovenia
Slovenia
whos
vnames={'Dataslcobs','Dataslinobs','Dataslmobs','Dataslpiobs','Dataslrobs'};
tData=array2table(temp,'VariableNames',vnames);
head(tData)
d=dir('S*cent*.mat'); % get a dir() for the file(s)
fn=d.name; % the name without having to type long name <vbg>
load('-ascii',fn)
whos
try
assert(mod(numel(Slovenia_centered),5)==0)
catch
error('Vector length (%d) not compatible; unable reshape to 5 variables. Aborting.', ...
numel(Slovenia_centered))
end
Slovenia_centered=reshape(Slovenia_centered,[],5); % reshape to 2D presuming are 5 variables
whos
vnames={'Dataslcobs','Dataslinobs','Dataslmobs','Dataslpiobs','Dataslrobs'};
for i=1:numel(vnames)
cmd=sprintf('%s=Slovenia_centered(:,i);',vnames{i}); % make a command to execute
eval(cmd); % and evaluate the string
end
whos D* % see who we made
return % just in here so online "run" won't error on next lines
Finally, I assume the obtained data in 5 columns is now saved in file Slovenia_centered.mat, right? I mean, using "load Slovenia_centered.mat" I obtain another error, and also my computer shows the last change to both Slovenia_centered.mat and Slovenia_centered.m were made weeks ago... the error:
Error using load
Unable to read file 'C:\[censored]\Slovenia_centered.mat'.
dpb
dpb le 21 Fév 2026 à 13:12
Modifié(e) : dpb le 21 Fév 2026 à 16:01
You've copied all the example code inserted just to show what was in the file as well as the actual code to do the work -- including the 'return' line that I commented that was there only so the subsequent example line of code to use in Octave to save the data from it i(Octave) as the five separate variables you had in it.
There's no code in what you ran that will save the ..mat file in MATLAB to then be able to read it.
You have to decide what it is that you're actually trying to do -- read the original Octave-written ASCII formatted file and create the five variables that we started with or build the data in MATLAB to begin with or precisely what are you trying to do now?
As @Stephen23 and I have both noted, if you were to change the save statement in Octave that created the existing file(s) to use -v7 instead of -ascii then you could just load that file in MATLAB and have the same variables by name in MATLAB directly without any other code being needed. But, if you continue to write files in the same way, then you need the code to create the variables the way you want them instead as one long vector. But, then, if you want to create a new .mat file you'll have to SAVE those in MATLAB; it won't somehow magically change the existing file.
The point is, you have to decide what is your objective here from what starting point and write code specific to those aims. You have to decide that; we can't do that for you.
Svit
Svit le 22 Fév 2026 à 14:20
I apologize. My aim is just to transform a 545x1 vector that I created in Octave to a 5x109 vector, so 5 columns with 109 rows of data each. That's all. All is one variable, called "temp". The five variables are defined later like this and I need no help with this:
Dataslpiobs=temp(:,1);
Dataslinobs=temp(:,2);
Dataslcobs=temp(:,3);
Dataslrobs=temp(:,4);
Dataslmobs=temp(:,5);
An example file is in attachment. Thank you so much!
dpb
dpb le 22 Fév 2026 à 15:21
Modifié(e) : dpb le 23 Fév 2026 à 15:14
We've already done that -- as first response showed, all you need to to is to load the file as -ascii instead of trying to read it as a binary .mat file.
The latter code works as well that generates the named variables from the variable name strings if you wnt to use that form instead.
The problem in the last code is that you tried to reread the .mat file yet again....it appears you're trying to do everything by simply copying from somebody else rather than actually reading and understanding the code.
ADDENDUM
NOTA BENE: If you ran the line of code
% OCTAVE CODE snippet...
...
save -V7 Slovenia_centered.mat Dataslcobs Dataslinobs Dataslmobs Dataslrobs Dataslpiobs
in MATLAB after the creation of the D* varaiables, you will have created a new .mat file in the -V7 format that is NOT ASCII any longer but in the native -V7 mat-file format. In that case, you can't read the new .mat file with the -ascii parameter but just with either the same -V7 parameter or without any parameter at all and you will retrieve the five variables as the separate variables.
That would also be true if you did do the same in Octave -- then the .mat file would be in the MATLAB .mat form and have the specific variables saved individually with their names.
in either of those cases, all you would need to do is write
load Slovenia_centered % load native MATLAB .mat file named Slovenia_centered.mat
This is, of course, much simpler and more direct to get the data you want as desired as @Stephen23 and I kept pointing out.
dpb
dpb le 23 Fév 2026 à 16:32
Modifié(e) : dpb le 23 Fév 2026 à 21:43
type Slovenia.m
temp=[0.067000000000000032 0.037379400869470003 0.18433509276973001 31.625314864252278 0.11012293770926 0.039000000000000055 -0.098394170124067995 -0.015833599156080999 16.429879165748627 0.072263225854672999 0.04200000000000003 -0.20897192803268 -0.17160003436134 17.253586158987421 0.014421521398178001 0.059999999999999998 -0.29283559910042001 -0.28770670889798 28.033902411104663 -0.032916262883566 0.038999999999999917 -0.10146584250649 -0.25036670679050999 19.001814229434107 0.0049062254220970996 0.046999999999999889 -0.20429439507207001 -0.32993983543960997 18.276585537724589 -0.054305047702654 0.036999999999999887 -0.26440474434417 -0.37066932633930999 16.178059503327205 -0.078131492314752998 0.045999999999999944 -0.31964282866976002 -0.40950773536436003 21.854836660928978 -0.11888495086585001 0.025000000000000001 -0.0022657586841141998 -0.12579270116769001 13.400393551190914 -0.14382118452993001 0.012000000000000028 -0.095910013338454003 -0.068395821076882996 4.9267771927383892 -0.15865466410793999 0.020999999999999942 -0.16742214225863999 -0.14130981443303001 5.7056161775867063 -0.18888841668010001 0.024999999999999859 -0.17761643106189001 -0.23741760397798001 10.829103467288945 -0.20153368607849001 0.032000000000000028 -0.22522551133553001 -0.35630283995394002 11.433980877431225 -0.22684656531196001 0.023000000000000003 -0.20402129374448999 -0.30616891494901999 13.200457955460386 -0.22413895062059 0.0010000000000000002 -0.19569258573024001 -0.27869030634421998 8.6720463102923535 -0.19869499685799 0.022000000000000002 -0.26225142120932998 -0.32993656079351003 5.7216922068153693 -0.22861064374699999 0.025000000000000001 -0.24891190198117999 -0.37749205748768 7.5727936225931103 -0.25565880821720999 0.028999999999999998 -0.19324850062603 -0.26940047412780999 8.3223589200638841 -0.24349508263361 0.012 -0.17950078489311 -0.27290394490782999 9.5253654746871117 -0.20272869929531001 0.019 -0.20778447010674 -0.27664085013593998 9.95074309945098 -0.19574114568376 0.029999999999999999 -0.18780579699354 -0.32151434120414002 10.205947749494156 -0.19421288978081999 0.017999999999999999 -0.18335740986480001 -0.20885464714794999 10.070329697222013 -0.14845159115310999 0.0030000000000000005 -0.17105040183035 -0.16920757669697001 7.802302879206624 -0.14362071963987 0.012000000000000002 -0.13680269573661999 -0.15418772538114001 6.1181169643276396 -0.14069776045318999 0.017000000000000001 -0.12140359754753 -0.18411379976461001 6.2733333333333334 -0.12298208253872001 0.0089999999999999993 -0.0049677344534507003 -0.090771002428379002 4.9233333333333329 -0.10612438962314 0.032000000000000001 -0.17797187464586001 -0.14827138549218 6.1366666666666667 -0.14164238392407999 0.019 -0.12117775980487 -0.094753022108476997 7.8033333333333337 -0.12186499819912 0.026000000000000002 -0.10883339019054999 -0.14214686580905 8.7866666666666671 -0.11759674896774 0.017000000000000001 -0.11590149404296 -0.076910656650236997 9.2066666666666688 -0.081466235936520998 0.025000000000000001 -0.086595884645343998 -0.082292930003423997 9.1066666666666674 -0.084342988500453006 0.018000000000000002 -0.079392024140489001 -0.057907180957338003 9.1066666666666674 -0.066246814419201994 0.027000000000000003 -0.088748692284989994 -0.10474785601495 8.8800000000000008 -0.058463783537209997 0.024 -0.080950220987433 0.0052932671921413002 9.2000000000000011 -0.0090143124243748998 0.011000000000000001 -0.045120125337948999 -0.011445580659398999 9.086666666666666 -0.012867644071298 0.0060000000000000001 -0.066668137147345 0.013156283753577001 7.3833333333333329 0.021074042169290999 0.032000000000000001 -0.092745547667948 -0.052253447800615999 7.8033333333333337 0.021112850961322999 0.017000000000000001 -0.076188528834755007 0.036005464762338998 8.4666666666666668 0.020152884524151 0.014999999999999999 -0.062472779216366001 0.018086196978288002 8.6333333333333329 0.012098414901814 0.0060000000000000001 -0.022747127280410999 0.025930583348817999 7.2000000000000002 0.040381268311121002 0.025000000000000001 -0.041201541729576997 -0.048969001717948997 7.580000000000001 -0.002216707385168 0.016 0.0010383769729954001 0.053982070487270002 6.8299999999999992 -0.012873891667434001 0.0039999999999999992 0.0063087906611922996 0.042007773440067002 6.4699999999999998 -0.013351975920894 0.0030000000000000005 0.012835983945688001 0.026407281392838999 6.2466666666666661 -0.028767179110874001 0.012999999999999998 0.028058788637644001 -0.056365882278379 5.8099999999999996 -0.058110282674146002 0.019 0.0069783779774139996 0.036856102386085998 4.7366666666666672 -0.070173237340126995 -0.002 0.045574213797647999 0.034261674533187 4.0366666666666662 -0.055139459809027 0.0019999999999999996 0.016226124733489 0.026050772876184 4.0700000000000003 -0.061575023424675003 0.013000000000000001 -0.014844206158443 -0.058811228119356997 4.0499999999999998 -0.066242550737069 0.0040000000000000001 0.019732429504136 0.065813246128702999 4.0499999999999998 -0.16760238204395 0.013000000000000001 -0.0026228414100515 0.025876174862435999 4.0300000000000002 -0.196109161285 -0.0050000000000000001 0.033346683900483003 0.011033468265979 4.0066666666666668 -0.14604138565107 0.0069999999999999993 0.00072768230242382004 -0.069913439239032996 3.7899999999999996 -0.11469636080789 0.014999999999999999 -0.0048093878415756 0.034943839774338001 3.4599999999999995 -0.095172710163122995 0.0069999999999999993 0.035467986382197003 0.019007711482967 3.4700000000000002 -0.064253413188418998 5.5511151231257828e-19 0.072506464829289005 0.0048145757131799003 3.6033333333333331 -0.039786729668775001 0.005000000000000001 0.031654398221884997 -0.038982152300721003 3.82033333333333 -0.053721518260111997 0.025000000000000001 0.028274338491444999 0.060887751226179003 4.0648333333333335 -0.043190229286851997 0.0040000000000000001 0.052246065299054997 0.065464552683143995 4.5004999999999997 0.018266575657361998 0.02 0.0033900292692380998 0.017345758180361 4.7247666666666666 -0.0070845290557794999 0.013000000000000001 0.051303369831173 -0.020589792895865999 4.4800000000000004 0.00069909376095367 0.027999999999999997 0.035258859538723999 0.064194004510487998 4.8604666666666665 -0.013579471739238 -0.0069999999999999993 0.033966170306456003 0.062378014928924003 4.9818000000000007 0.029727126839218 -0.016 -0.085748597659701997 0.0092009401987986992 4.214666666666667 0.058227753069685001 0.011000000000000001 -0.21234533809303999 -0.088292429009035001 2.0116999999999998 0.045925555341059 0.013000000000000001 -0.21550343295612001 -0.0099519738128020004 1.3106333333333333 0.045396880080193998 -0.0080000000000000002 -0.18447501114709 0.022058562330121999 0.86920000000000008 0.038767997070615998 0.0050000000000000001 -0.21009337053298999 -0.013522578879816 0.7219000000000001 0.014929464115394 0.0069999999999999993 -0.24333673888578 -0.10529325954528999 0.66213333333333335 0.0038098898122810999 0.017000000000000001 -0.181664917137 -0.01522932467374 0.68626666666666658 -0.0059461969700933997 -0.0090000000000000011 -0.14559929720133999 0.017794714641837999 0.87493333333333323 0.012099147327446999 0.0050000000000000001 -0.10581975146496 -0.011554859153243001 1.0204666666666666 -0.0067927876356713001 0.0099999999999999985 -0.088672669175020993 -0.10626781366497 1.0931333333333333 -0.016164712782643 0.0080000000000000002 -0.078504419821289001 -0.0083473426333702007 1.4116333333333333 -0.013793831399934 -0.0020000000000000005 -0.048513482864520001 0.022921742379013 1.5620666666666665 0.0021082647479104999 0.0050000000000000001 -0.019207632780079999 -0.0081487141500193993 1.4955666666666667 -0.0028495089865181 0.013000000000000001 -0.041710931458545999 -0.090736567131881996 1.0429999999999999 -0.0073873281217836 0.0090000000000000011 -0.029483241165686999 -0.029779190707726 0.69603333333333328 -0.0069668283719934998 0.01 0.00041846761090447003 -0.0066951777386722996 0.35856666666666664 -0.017320766085350998 -0.0010000000000000002 -0.0071406402814249001 -0.0086027498926695006 0.19513333333333335 -0.021942120839813999 0.0049999999999999992 0.068113246684096004 -0.11629607534965 0.21146666666666666 -0.024511902136402999 0.0089999999999999993 0.051547955485397999 -0.030675323276014001 0.20679999999999998 -0.033837404697922999 0.0040000000000000001 0.054518552294269003 0.0065763064421977997 0.2235 -0.027381682343935999 -0.0069999999999999993 0.094878347986101993 0.020785075755057999 0.2409 -0.028277811843547999 0.0009999999999999998 0.078259766305579001 -0.074598895195357007 0.2951333333333333 -0.02170168981386 0.013000000000000001 0.060586902349478 -0.0084770380065917004 0.2985666666666667 -0.017606892411101999 -0.0080000000000000002 0.078864146007737002 0.028473041251463001 0.16456666666666667 0.017756025574367999 -0.0069999999999999993 0.032697589718012003 0.020827458938114999 0.081466666666666673 0.0097786761957863004 -0.0009999999999999998 0.056247216389234002 -0.061268891366963001 0.046033333333333336 0.0068990408934545997 0.0069999999999999993 0.045727088198640999 0.011783825468731 -0.0065333333333333328 0.0038665447159047998 -0.0080000000000000002 0.049553742796544002 0.040253704861668002 -0.027800000000000002 0.021178147529934001 -0.002 0.045352397958228999 0.025891037238841001 -0.089166666666666658 0.013837648958383001 -0.0059999999999999984 -0.017092886037471 -0.047987891668625 -0.18606666666666669 0.029214550894596999 0.016 -0.0075663467091890996 0.015616055437724 -0.2581 0.013095551579717001 -0.0069999999999999993 0.030701039487054001 0.066595219637543002 -0.29809999999999998 0.038518044513572998 0.0009999999999999998 0.040317052471765001 0.053797554307763 -0.3125 0.034919109316394997 0.0089999999999999993 0.061196840998367003 -0.052808034286070997 -0.32779999999999998 0.033402357870845001 0.0060000000000000001 0.067855974614152006 0.022828347872613002 -0.32996666666666669 0.029137530876316001 -0.002 0.062338435558049 0.056611468893191998 -0.32963333333333339 0.034372739559412002 0.0069999999999999993 0.092882060707471001 0.057731682904596002 -0.32880000000000004 0.026194398669709001 0.0040000000000000001 0.076429080290760995 -0.045962246694870999 -0.32830000000000004 0.028312459904814 0.013999999999999999 0.069579469925841006 0.022135532109809002 -0.32523333333333332 0.020238508699176001 -0.0030000000000000005 0.093328734844229003 0.065924937073952999 -0.31949999999999995 0.033725911818923003 -0.0009999999999999998 0.085839940209259002 0.074628606728460001 -0.31533333333333335 0.026419729129852 0.0049999999999999992 0.069372583082371 -0.061236873363729002 -0.30853333333333333 0.022837211903472002 0.018000000000000002 0.041208160425672997 0.019815149021349 -0.31709999999999999 0.01365543700172 -0.0039999999999999992 0.021557016421538001 0.076524591751176999 -0.39673333333333333 0.031825490194894002 0 -0.022200471431363 0.049669756215859 -0.4029666666666667 0.014494388299074 -0.0069999999999999993 -0.043437691342452997 -0.13002122863481 -0.40549999999999997 0.0035094944534535002]; Dataslpiobs=temp(:,1); Dataslinobs=temp(:,2); Dataslcobs=temp(:,3); Dataslrobs=temp(:,4); Dataslmobs=temp(:,5);
Well, certainly pasting in data manually into an m-file isn't "the MATLAB way". I don't see what is the issue that has you stymied, but starting with the Octave-generated (I presume?) file, the prior code works just fine...
fn='Slovenia_centered.mat'; % set the file name -- change as needed for other files
load('-ascii',fn) % assume was written with -ascii switch in Octave so is vector
% the loaded variable will be the same as the name of the .mat file, "Slovenia_centered"
%whos
try % make sure can split into five variables
assert(mod(numel(Slovenia_centered),5)==0)
catch
error('Vector length (%d) not compatible; unable reshape to 5 variables. Aborting.', ...
numel(Slovenia_centered))
end
Slovenia_centered=reshape(Slovenia_centered,[],5); % reshape to 2D presuming are 5 variables
vnames={'Dataslcobs','Dataslinobs','Dataslmobs','Dataslpiobs','Dataslrobs'};
for i=1:numel(vnames)
cmd=sprintf('%s=Slovenia_centered(:,i);',vnames{i}); % make a command to execute
eval(cmd); % and evaluate the string
end
clear Slovenia_centered % remove the vector copy -- no need to keep two copies of same data
At this point you will have the five D* variables by the names given in the vnames list -- change it to correct any typos or make other desired/needed changes, if any were ever required. The code does presume and require that the size of the imported data vector be evenly divisible by 5; if that were to ever change, the appropriate changes would be required in the testing code and the names array to match.
Above you wrote "I assume the obtained data in 5 columns is now saved in file Slovenia_centered.mat, right?"
The answer to that is a resounding "NO!". Rearranging the data in memory has nothing to do with changing the format or arrangement of the data in the disk file.
As noted in the prior comment, IF you were to save the data now into a .mat file, you could write it that way, yes. But, having done so, then you don't need (and can't use) this code to read the -ascii version. That would not be a bad thing if this has need of being done more than once, but you would then have to be sure to use the right code for the job. There isn't a specific builtin function in MATLAB to query the internal form of a .mat file, but one can query the content of variable(s) in the file using the whos command with the -file argument to find out if the D* variables are there or not and choose which code to run based on that...
% save the specific D* variables in native MATLAB .mat file
save Slovenia_centered.mat Dataslcobs Dataslinobs Dataslmobs Dataslrobs Dataslpiobs
The above replaces the original .mat file in ASCII vector format with the native binary format and keeps the five variables separate.
dir *.mat % see what is there -- is same file name as before
Slovenia_centered.mat
v=whos('-file',fn,'D*') % query the file content of variabless beginning with "D"
v = 5×1 struct array with fields:
name size bytes class global sparse complex nesting persistent
and we see there really are five of them as expected.
So, we could write code something like(*)
if numel(whos('-file',fn,'D*'))==5 % see if we have five D* variables in the file
load fn % if so, load them directly
else
% the above code to read the Octave -ascii version and create the D*
% variables would go in here...as well as the |save| command so that only
% do it when starting with the -ascii version.
end
(*) The above checks if the file contains fiive (5) variables whos names start with "D". An alternative way would be to just try to read the file as -ascii as was originally and trap the error and then presume is the desired format...
try % presume is the -ascii version first
% the above code to read the Octave -ascii version and create the D*
% variables would go in here...as well as the save command so that only
% do it when starting with the -ascii version.
catch
load(fn) % if not, assume can load them directly
end
it's in the eye of the beholder which to use...
dpb
dpb le 23 Fév 2026 à 18:49
Modifié(e) : dpb le 23 Fév 2026 à 21:44
ADDENDUM SECOND:
Of course, the neat way to code this would be to encapsulate the work to read the -ascii version in a function -- that hides the dirty details from the top level code...
function [Dataslcobs Dataslinobs Dataslmobs Dataslpiobs Dataslrobs] = readASCIImat(fn)
% read Octave-generated ASCII .mat file as vector and return five specific variables
% USAGE
% [Dataslcobs Dataslinobs Dataslmobs Dataslpiobs Dataslrobs] = readASCIImat(file)
load('-ascii',fn) % assume was written with -ascii switch in Octave so is vector
try % make sure can split into five variables
assert(mod(numel(Slovenia_centered),5)==0)
catch
error('Vector length (%d) not compatible; unable reshape to 5 variables. Aborting.', ...
numel(Slovenia_centered))
end
Slovenia_centered=reshape(Slovenia_centered,[],5); % reshape to 2D presuming are 5 variables
vnames={'Dataslcobs','Dataslinobs','Dataslmobs','Dataslpiobs','Dataslrobs'};
for i=1:numel(vnames)
cmd=sprintf('%s=Slovenia_centered(:,i);',vnames{i}); % make a command to execute
eval(cmd); % and evaluate the string
end
% save the specific D* variables in native MATLAB .mat file
save Slovenia_centered.mat Dataslcobs Dataslinobs Dataslmobs Dataslrobs Dataslpiobs
end
Then the above logic in your main program would look like
fn='Slovenia_centered.mat'; % set the file name -- change as needed for other files
try % presume is the -ascii version first
[Dataslcobs Dataslinobs Dataslmobs Dataslpiobs Dataslrobs] = readASCIImat(fn);
disp(['Reading -ASCII .mat file ' fn])
catch
disp(['Loading native .mat file ' fn])
load(fn) % if not, assume can load them directly
end
Reading -ASCII .mat file Slovenia_centered.mat
% Have the specific five D* variables in memory now
% Just to show it does what want, we'll make into a table so can use head()
% conveniently to show resutls...this is only for show, not necessary
% unless you find a use for the table as well...
tS=table(Dataslcobs,Dataslinobs,Dataslmobs,Dataslpiobs,Dataslrobs);
head(tS,5)
Dataslcobs Dataslinobs Dataslmobs Dataslpiobs Dataslrobs __________ ___________ __________ ___________ __________ 0.24359 0.089221 0.15389 26.45 0.054688 0.043417 -0.046553 0.11604 11.254 0.026688 -0.11235 -0.15713 0.058193 12.078 0.029688 -0.22846 -0.24099 0.010856 22.858 0.047688 -0.19112 -0.049624 0.048678 13.826 0.026688
% Whatever else you need to do comes next goes after...
Now, let's do this again and see what happens...
try % presume is the -ascii version first
[Dataslcobs Dataslinobs Dataslmobs Dataslpiobs Dataslrobs] = readASCIImat(fn);
disp(['Reading -ASCII .mat file ' fn])
catch
disp(['Loading native .mat file ' fn])
load(fn) % if not, assume can load them directly
end
Loading native .mat file Slovenia_centered.mat
tS=table(Dataslcobs,Dataslinobs,Dataslmobs,Dataslpiobs,Dataslrobs);
head(tS,5)
Dataslcobs Dataslinobs Dataslmobs Dataslpiobs Dataslrobs __________ ___________ __________ ___________ __________ 0.24359 0.089221 0.15389 26.45 0.054688 0.043417 -0.046553 0.11604 11.254 0.026688 -0.11235 -0.15713 0.058193 12.078 0.029688 -0.22846 -0.24099 0.010856 22.858 0.047688 -0.19112 -0.049624 0.048678 13.826 0.026688
We see it read the -ascii file the first pass as started with a copy of the original posted file. The second time it read the rewritten native format file instead since the first past rewrote it. Note that the disp() call is after the attempt to read the file so the error occurred trying to read as ascii so the catch clause was entered before reaching the disp() call.
dpb
dpb le 24 Fév 2026 à 14:37
ADDENDUM THIRD:
function [Dataslcobs Dataslinobs Dataslmobs Dataslpiobs Dataslrobs] = readASCIImat(fn)
% read Octave-generated ASCII .mat file as vector and return five specific variables
% USAGE
% [Dataslcobs Dataslinobs Dataslmobs Dataslpiobs Dataslrobs] = readASCIImat(file)
load('-ascii',fn) % assume was written with -ascii switch in Octave so is vector
try % make sure can split into five variables
assert(mod(numel(Slovenia_centered),5)==0)
catch
error('Vector length (%d) not compatible; unable reshape to 5 variables. Aborting.', ...
numel(Slovenia_centered))
end
Slovenia_centered=reshape(Slovenia_centered,[],5); % reshape to 2D presuming are 5 variables
vnames={'Dataslcobs','Dataslinobs','Dataslmobs','Dataslpiobs','Dataslrobs'};
for i=1:numel(vnames)
cmd=sprintf('%s=Slovenia_centered(:,i);',vnames{i}); % make a command to execute
eval(cmd); % and evaluate the string
end
% save the specific D* variables in native MATLAB .mat file
save Slovenia_centered.mat Dataslcobs Dataslinobs Dataslmobs Dataslrobs Dataslpiobs
end
NOTA BENE: The above works only for fn being specifically 'Slovenia_centered.mat' and for the specific save statement as written in Octave such that the variable name on loading the file is the same as the name of the file itself (sans extension, of course). To handle other file names one would have to use the variable assignment of load such that the data are returned in a struct and the name of the variable is a field name in the struct that could then be referred to programmatically rather than as a fixed variable name as in the above code.

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 13 Fév 2026 à 14:50
load "Slovenia_centered.mat"
Using the command syntax the filename is text already so it's including the apostrophes as well...try
load Slovenia_centered.mat
I'm not sure about what level for .mat file compatibility Octave supports, you may need to check there about ensuring .mat files.

4 commentaires

Stephen23
Stephen23 le 13 Fév 2026 à 14:58
Modifié(e) : Stephen23 le 13 Fév 2026 à 15:13
Yes. The documentation explains why using double quotes will fail with command syntax, it has whole section entitled "Why Does Using Command Form With Strings Return An Error?":
The gist of it is "In command form, double quotes are treated as part of the literal text rather than as the string construction operator". This is to be consistent with how command syntax has always worked.
dpb
dpb le 13 Fév 2026 à 15:22
BTW, to complete the picture
load("Slovenia_centered.mat")
will also work because as an argument the "" are constructors and the underlying char string will be passed.
Stephen23
Stephen23 le 13 Fév 2026 à 15:44
According to the Octave SAVE documentation
its default is to "Save the data in Octave’s text data format. (default)." Apparently it also has options to save in formats which are MATLAB compatible.
Andiswa
Andiswa le 27 Fév 2026 à 5:06
Use single quote

Connectez-vous pour commenter.

Catégories

Produits

Version

R2025b

Tags

Question posée :

le 13 Fév 2026 à 14:32

Modifié(e) :

le 27 Fév 2026 à 15:26

Community Treasure Hunt

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

Start Hunting!

Translated by