The documentation for the %store magic states that you can store your variables to a file.
http://ipython.org/ipython-doc/rel-0.12/config/extensions/storemagic.html
ie: %store foo >a.txt - Store value of foo to new file a.txt
%store foo >>a.txt - Append value of foo to file a.txt
I have been able to successfully write the variables to a file, which is readable in plain text. (although it appears that it leaves out some data, since there are ellipse).
How do i restore these variables? The typical command is %store -r, but %store doesn't show any variables.
I can't find in the documentation how to "load" from file after using %store. I guess the %store -r var_name is only meant when you saved on the "default" storage used by %store.
https://ipython.readthedocs.io/en/stable/config/extensions/storemagic.html
But here are 2 ways. I'd love to see a better one.
Saving
Loading with %load
%load file_to_load
Load "manually" (just one way)
x=!cat out_file.txt
x
y=eval(''.join(x)) # Always be careful when using 'eval'
y
Related
I am using MATLAB R2012a to read an hdf5 data file using the the function h5disp. How can I assign the output to a variable or write it to disk? Is there a way to redirect the output in MATLAB? My goal is to read in many files and output a summary of their contents to disk in a format equal to or similar to h5disp.
Like
h = output(h5disp(myhdf5file))
or to disk like
h5disp(myhdf5file) > outputfile.txt
You can use h5info to store that information in the workspace i.e.
h = h5info(myhdf5file);
or you can use diary to write the output of h5disp to a text file.
diary outputfile.txt
h5disp(myhdf5file)
diary off
I am creating a matlab application that is analyzing data on a daily basis.
The data is read in from an csv file using xlsread()
[num, weather, raw]=xlsread('weather.xlsx');
% weather.xlsx is a spreadsheet that holds a list of other files (csv) i
% want to process
for i = 1:length(weather)
fn = [char(weather(i)) '.csv'];
% now read in the weather file, get data from the local weather files
fnOpen = xlsread(fn);
% now process the file to save out the .mat file with the location name
% for example, one file is dallasTX, so I would like that file to be
% saved as dallasTx.mat
% the next is denverCO, and so denverCO.mat, and so on.
% but if I try...
fnSave=[char(weather(i)) '.mat'] ;
save(fnSave, fnOpen) % this doesn't work
% I will be doing quite a bit of processing of the data in another
% application that will open each individual .mat file
end
++++++++++++++
Sorry about not providing the full information.
The error I get when I do the above is:
Error using save
Argument must contain a string.
And Xiangru and Wolfie, the save(fnSave, 'fnOpen') works as you suggested it would. Now I have a dallasTX.mat file, and the variable name inside is fnOpen. I can work with this now.
Thanks for the quick response.
It would be helpful if you provide the error message when it doesn't work.
For this case, I think the problem is the syntax for save. You will need to do:
save(fnSave, 'fnOpen'); % note the quotes
Also, you may use weather{i} instead of char(weather(i)).
From the documentation, when using the command
save(filename, variables)
variables should be as described:
Names of variables to save, specified as one or more character vectors or strings. When using the command form of save, you do not need to enclose the input in single or double quotes. variables can be in one of the following forms.
This means you should use
save(fnSave, 'fnOpen');
Since you want to also use a file name stored in a variable, command syntax isn't ideal as you'd have to use eval. In this case the alternative option would be
eval(['save ', fnSave, ' fnOpen']);
If you had a fixed file name (for future reference), this would be simpler
save C:/User/Docs/MyFile.mat fnOpen
I have some data in a cell array,
data2={[50,1;49,1;26,1];...
[36,2;12,2;37,2;24,2;47.3,2];}
and names in another cell array,
names2={'xxx/01-ab-07c-0fD3/0';'xxx/01-ab-07s-0fD3/6';}
I want to extract a subset of the data,
data2_subset=data2{1,:}(:,1);
then a temporary file name,
tempname2=char(names2(2));
an save the subset to a text file with
save (tempname2, 'data2_subset', '-ASCII');
But I get this error message: _
Error using save
Cannot create '6' because 'xxx/01-ab-07s-0fD3' does not exist.
To try to understand what is happening, I created a mock dataset with simpler names:
names={'12-05';'14-03'};
data={[50,1;29,1;25,1];[35,2;22,2;16,2;38,2];[40,3;32,3;10,3;44,3;43,3];};
data_subset=data{1,:}(:,1);
tempname=char(names(2));
save (tempname, 'data_subset', '-ASCII');
in which case the save command works properly.
Unfortunately I still do not understand what the problem is in the first case. Any suggestions as to what is happening, and of possible solutions?
MATLAB is interpreting the the forward slashes (/) as directory separators and 6 as the intended file name (your second example doesn't have this slash problem).
Since the relative directory tree xxx/01-ab-07s-0fD3/ doesn't exist, MATLAB can't create the file.
To solve the problem, you can either create the directories beforehand using mkdir():
>> pieces = strsplit(tempname2,'/');
>> mkdir(pieces{1:2});
>> save(tempname2, 'data2_subset', '-ASCII');
or replace the / with some other benign symbol like _:
>> tempname3= strrep(tempname2,'/','_');
>> save (tempname3, 'data2_subset', '-ASCII');
(which works for me).
I have 31 models an I want to save each one in a specific file
this is my matlab function
formatspec='model%d'
for k = 1:length(libsvmFiles)
baseFileName = libsvmFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
[labels train]=libsvmread(fullFileName);
model=svmtrain(labels,train, '-t 2 -h 0');
file=sprintf(formatspec,k);
save file model;
but the problem is only the first file is saved and its name is 'file' tha's mean the value of the variable file is not evaluated
how can I solve this problem ?
As many Matlab functions, save can be used in function form (save(...)) or in command form (save ...). In the command form that you use, all the arguments are interpreted as strings. That means
save file model
is equivalent to
save('file', 'model')
For the second argument that is correct, because you want to refer to the variable with the name "model". For the first argument it is wrong, because you want to refer to the file name contained in the variable file. The correct syntax to use is therefore
save(file, 'model')
You're missing the parens for the save function. The model variable also needs to be listed as a string since you need to tell the save function the name of the variable, not the variable itself. See Matlab's documentation.
save(file, 'model');
Additionally you don't have an end to your for loop shown, which normally would just throw an error -- however later code might cause this loop to instead only run once. Otherwise you should check your libsvmFiles variable as it might be only length 1 or not be an array.
My MATLAB script is to:
Extract four different fMRI onsets from MATLAB files (the files are named 'subject 06 data', 'subject 05 data', etc.)
Put this information in a new file with two other variables named 'durations' and 'names'.
Save all this as a new MATLAB file.
I am facing two problems:
At the moment, the script below manages to do steps 1 through 3 for the first MATLAB file in the directory 'Gender_recogntion', but it does not do 1 through 3 for the other MATLAB files in the folder. It crashes in the loop at the line 'load(sub_name(i).name);'.
This is the error I get:
??? Improper index matrix reference.
Error in ==> Gender_onsets_script_2 at 16
load(sub_name(i).name);
In addtion, I would like to name the new MATLAB files with the name of the original MATLAB files. At the moment, the new MATLAB files is named 'onsets.mat'.
clear all
close all
clc
cd 'C:\Program Files\MATLAB\R2007b\Data\Resilience\Real_data\Raw\Matlab_files\Gender_recogntion';
sub_name = dir('C:\Program Files\MATLAB\R2007b\Data\Resilience\Real_data\Raw\Matlab_files\Gender_recogntion\*.mat');
for i = 1:numel(sub_name);
load(sub_name(i).name);
names = {'sad' 'anger' 'neutral' 'rest'};
durations = {[18] [18] [18] [18]};
onsets=cell(1,4);
onsets{1} = data.time_since_scan_start(data.emotion==5)/1000; %Get the 36 onsets for sad.
onsets{2} = data.time_since_scan_start(data.emotion==4)/1000; %Get the 36 onsets for anger.
onsets{3} = data.time_since_scan_start(data.emotion==6)/1000;% Get the 36 onsets for calm.
onsets{4} = datarest.onset/1000; %Get the six onsets for the rest blocks.
onsets{1} = onsets{1}(1:6:36)'; %Get the first onset value of each of the six blocks.
onsets{2} = onsets{2}(1:6:36)';
onsets{3} = onsets{3}(1:6:36)';
onsets{4} = onsets{4}';
%cd Onsets folder, saves onsets, and then cd back to folder "Matlab_files"
cd 'C:\Program Files\MATLAB\R2007b\Data\Resilience\Real_data\Onsets';
save 'onsets.mat' names durations onsets
cd 'C:\Program Files\MATLAB\R2007b\Data\Resilience\Real_data\Raw\Matlab_files\Gender_recogntion';
end
For your second question about naming the output files the same as the input, you can use the function version of save and pass in the variable sub_name(i).name as the filename argument.
save(sub_name(i).name, 'names', 'durations', 'onsets')
This uses the exact same name for input and output (in different directories, in your script). When I save output files, I typically keep them in the same directory as the inputs, so I modify an input filename with regular expressions (see regexprep) or adding a prefix or suffix (strcat) to create a related but distinct output filename.
For future reference...the default filetype for save is MATLAB data format; you could pass in '-ASCII' as an argument to save as a text file if your data types were compatible. The cell arrays in this example aren't, but strings and numerical matrices would be, so if text output files were important, you could use alternate data structures from the beginning or convert cells with cell2mat. A generic example with the save() version: save(filename, '-ASCII', 'x', 'y','z') where x,y,z are ASCII-friendly variables and filename is a text file.
[additional response, adding Jan 5, 2011]
About your first question on the error message:
??? Improper index matrix reference.
Is it possible that a saved .mat file contains a variable named dir, that would override the standard directory-listing function and cause that error? I read that tip on another site, just wanted to pass it along in case it helps.