Wed 06 Jul 2016 03:01:52 PM UTC, comment #7:
I do agree it is low priority, but it's worth fixing.
In terms of workaround, it is just simpler to load the entire file.
|
Tue 05 Jul 2016 10:00:10 PM UTC, comment #6:
The easy way to update variables is not to use Matlab ;)
-append certainly works in Octave by itself, and in Matlab by itself. If you want to swap data files between the two then just make sure you write out all the variables you need.
For example,
jwe's suggestion works fine. But load/save are both implemented in C++ rather than as m-files. This means it is not quite as simple to code up, even though the concept is fine.
You could always write your own wrapper around save. Something vaguely like this:
|
Tue 05 Jul 2016 09:30:21 PM UTC, comment #5:
You can do what Rik suggests in comment #3. But I think you can store the variables obtained from the load function in a struct, modify the struct with the new variable(s) that you are appending, then write the struct out to the same file that you just loaded.
Maybe you could do better in C++.
In any case, it is also low priority for me unless someone wants to use $$ to increase my interest.
|
Tue 05 Jul 2016 09:13:27 PM UTC, comment #4:
I am the original submitter of the bug report.
I don't see how else you can update variables in a file.
You have to use -append and Octave should overwrite the existing variable.
I would say it is well worth fixing.
It might be hard to do though...
And I would also say that efficiency is a big deal.
I have no idea how this could/should be done, but maybe erasing the previous versions of the variables that should be overwritten (leaving empty spaces within the file), might be an option.
|
Tue 05 Jul 2016 04:14:39 PM UTC, comment #3:
Mike is correct. Matlab specifically mentions that Matlab overwrites an existing variable when using -append.
This is annoying, and possibly not worth fixing. I'm pretty sure Octave just relies on opening the file in 'append' mode and adding the data at the end of the file.
The alternative, which Matlab seems to do, is to open the file for reading, parse through all contents, and copy all the unduplicated variables to a new file, save in the new or duplicated appended variables, close the file, and then rename the new temporary file to the old one.
If efficiency isn't a big deal, it might be possible to work at the m-file level in a separate namespace. First, clear the namespace of all variables. Next, load in the existing file to get all of the variables in the file. Use evalin() or somesuch to import the variables that are to be appended. Finally, write out all variables in the temporary namespace to the file. But, if you have 2GB arrays, then this is going to be cumbersome.
|
Tue 05 Jul 2016 03:47:59 PM UTC, comment #2:
I've changed the Item Group to Matlab Compatibility, and marked it as minor since there is the obvious workaround of loading the whole file.
The sequence of commands works as long as one stays in Octave. This leads me to believe that it is Matlab which has the issue. My guess is that even for '-append', they may be checking whether the variable exists and replacing the variable, rather than appending it.
In Octave, I checked the size of the generated file after every save command.
You might try the entire sequence under Matlab and see if the file always grows, or whether they are replacing variables.
The other thing to try is changing the b variable to something which is of a different size or type in Octave. Repeat the first steps you did to create datafile.mat in Matlab. But in Octave, try
Then try loading the file in Matlab. If Matlab is really just using the first instance of a 'b' variable that it finds in the file then this will still be zeros(2,2).
|
Tue 05 Jul 2016 03:33:05 PM UTC, comment #1:
Thank you for contributing this bug report.
In Octave, the following
results in a file that contains one copy of "a" and two copies of "b" with different values.
In Octave, loading from the file always retrieves the last value of "b", either with
So the primary bug seems to be that Octave does not overwrite a variable with the same name in the file if the -append option is given. This is explicitly mentioned in Matlab's help for save at https://www.mathworks.com/help/matlab/ref/save.html.
|
Tue 05 Jul 2016 12:26:43 PM UTC, original submission:
In MATLAB R2015a (under Ubuntu 12.04) I created a mat file so that I could read it in Octave:
>> a=1;
>> save('datafile.mat', 'a', '-v7');
I then created a matrix and added it to the file:
>> b=zeros(2,2);
>> save('datafile.mat', 'b', '-append', '-v7');
In Octave 4.0.3 I loaded the file, changed the contents of b and saved the modified version to file.
load('datafile.mat');
> b(1,1)=1;
> save('datafile.mat', 'b', '-append', '-v7');
Back in MATLAB, I tried to load the variable from the file:
>> load('datafile.mat', 'b')
>> b
b =
0 0
0 0
It loads the first version of b.
If I load the full file, it loads correctly:
>> load('datafile.mat')
>> b
b =
1 0
0 0
Of course, one might argue that this is a MATLAB bug, but the same code in MATLAB produces the correct result.
|