1
votes

I'm trying to get the Save/Load command to use a variable as the path in octave/matlab I can save variables using the Save/Load command

save('/tmp/a1_var.mat','V1','V2')

But the path I use will change based on the location of the file. How can I have the save/load command accept variables for the path/and or filename?

Example:

dirpath='/tmp/';
save(dirpath,'a1_var.mat','V1','V2')

I tried

save(strcat(dirpath),'a1_var.mat','V1','V2')

but I get an error save: unable to open output file

1
Tried save(strcat(dirpath,'a1_var.mat'),'V1','V2')? I think fullfile might be safer instead of strcat.Divakar
@Divakar Thanks that fixed itRick T

1 Answers

1
votes

The input to save is comma separated so your command

save(dirpath,'a1_var.mat','V1','V2')

is trying to save a variable called 'a1_var.mat' as this is after the first comma. The error message is because you have defined the filename as just the folder '/tmp/' rather than a file.

You need to group your pathname and filename into one string using square brackets []

save([dirpath,'a1_var.mat'],'V1','V2')