How to load data from Octave?

76 views (last 30 days)
Svit
Svit on 13 Feb 2026 at 14:32
Edited: dpb 7 minutes ago
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"
^^^^

Accepted Answer

dpb
dpb on 13 Feb 2026 at 16:06
Edited: dpb on 13 Feb 2026 at 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).
  5 Comments
dpb
dpb about 4 hours ago
Edited: dpb 7 minutes ago
"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 wo the file itsel had five columns. Unless there are other files, that isn't true and so the previous assumptions all are in error.
d=dir('Slov*cent*.mat'); % get a dir() for the file(s)
fn=d.name; % the name without having to type long name <vbg>
load('-ascii',d.name)
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
temp=reshape(Slovenia_centered,[],5); % reshape to 2D presuming are 5 variables
whos
Name Size Bytes Class Attributes Slovenia_centered 545x1 4360 double ans 1x45 90 char d 1x1 960 struct fn 1x21 42 char temp 109x5 4360 double
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.
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=temp(:,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
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 temp array 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 about 1 hour ago
"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!

Sign in to comment.

More Answers (1)

dpb
dpb on 13 Feb 2026 at 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.
  3 Comments
dpb
dpb on 13 Feb 2026 at 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 on 13 Feb 2026 at 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.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products


Release

R2025b

Community Treasure Hunt

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

Start Hunting!