Saving file in different directory than than loading dir.

99 views (last 30 days)
I would like to save files dirrectly after some operations in different directory than I loading directory. Is there any way to do in efficiently.
My code :
cd('C:\Users\Jonasz\Documents\Studia_Biotechnologia\_Projekt\N');
save(Names,'newy');
cd(PathName);
where PathName - loading directory.
I think is without sense to open each time other directory and than open second one each time in a for loop ( loop is very long , n is 10 thousand) Is there any way to put it inside save command or something else more efficient.

Accepted Answer

dpb
dpb on 12 Aug 2013
There's no need to 'cd' at all...just build a fully qualified file name
savdir = 'C:\Users\Jonasz\Documents\Studia_Biotechnologia\_Projekt\N';
save(fullfile(savdir,Names),'newy');
doc fullfile % for details
--
  1 Comment
dpb
dpb on 16 Jan 2019
A struct is a data abstraction and needs must be "serialized" to be printed in other than a display format. Matlab has the save function that writes .mat files which can then be read back into Matlab via load and recreate the variables as they were in the workspace, including structures, tables, cell arrays, etc., etc., ...
But, if you try to use the -ascii option to write the same structure in text format, then what is written is all the data within the structure in a serial string...example:
tmp.A=pi; % create a simple struct
tmp.B=rand(3,1); % to illustrate serialized i/o
save tmp.txt -struct tmp -ascii % save as ascii with SAVE
>> type tmp.txt % see what we got...
3.1415927e+00
9.5716695e-01
4.8537565e-01
8.0028047e-01
>> tmp
tmp =
struct with fields:
A: 3.14
B: [3×1 double]
>>
You see we got the content but the two fields are lost; just the four numbers in sequence in the file...otoh
>> save tmp.mat tmp % save the struct as .mat file
>> clear tmp % clear the local variable
>> load tmp % retrieve it from the .mat file
>> tmp % and Voila! back in all its glory...
tmp =
struct with fields:
A: 3.14
B: [3×1 double]
The best high-level routine for such purposes if can't use the native storage file formats for some reason is to convert the data structure to a table and use writetable if the structure is such that each field has the same number of rows as it converts each field to a variable in a table.
If that doesn't hold then struct2cell will turn each field into a separate cell in a cell array and it could be then converted to table.
All in all, this is a problem every programming language has with these data abstractions in how to represent externally.

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!