How do I load a .mat file into a function when the file is named as a number string which is represented by a variable in the workspace from a previous process?

I have a script which analyses a photograph and saves the data into a .mat file. Each photograph in the folder is named a certain number, and the script prompts the user to input the reference number associated with their photograph of choice. This is then stored in a variable 'ref' as a string: e.g ref = '850'.
The script then proceeds to save the data in this case as 850.mat.
I then want to use this in a function, so to load the correct .mat file, I have been trying to load it in the following way:
load ref.mat - In this way, the previously stored ref variable which is present in the workspace as '850' would essentially load 850.mat, corresponding to the mat file I created previously.
Unfortunately this doesn't work and nothing I have tried (spent 3 hours searching for a solution) seems to work. The code is shown below:
*ref = num2str(input('Please enter reference number: '));*
After some calculations and manipulation...
*save(ref, 'timedata', 'tempdata');*
saving what in the previous example was 850.mat
If i now want to select that file to load into a function, what would I type so it always calls upon the file corresponding to the ref variable?
Thanks for your help in advance, and I hope I explained the question well enough!
Nik

 Accepted Answer

S = load([ref,'.mat'])

1 Comment

This (with all your help from before) works perfectly!
Thank you so much for your help and sorry for the initial confusion on my part!

Sign in to comment.

More Answers (1)

load( fullfile( ref, '.mat' );
or some variation of that if you want to load specific variables or assign the output to a struct rather than just spew them straight into the workspace, overwriting any of the same name that already exist.

8 Comments

This gives me the same error message of:
"Undefined function or variable 'ref'." - I know I need to declare the variable in the functions own workspace, but I didnt want to have to input it to the function arguments every time...
The closest I have come is to use this within the function:
ref1 = evalin('base','ref');
load(fullfile(ref1,'.mat'));
But then I get this error message:
Error using load
Unable to read file '͒\.mat'. No such file
or directory.
@Nikolaos Imellos: you wrote that "...the previously stored ref variable which is present in the workspace as '850' ..." and "This is then stored in a variable 'ref' as a string: e.g ref = '850'."
And now you are telling us that ref is not defined in the workspace. So, is ref defined (as your question states), or is ref not defined (as you are now telling us)?
If you are not sure what is in the workspace, then please check if you call clear or the like (something that beginners often like to do), and what these commands show:
exist ref
whos ref
yes, it was my bad, I'm sorry - I thought the ref in the workspace meant it was present in the functions own workspace, but I realised that wasn't the case, apologies for the confusion, i'm quite new to MATLAB :S
I used the evalin function as I said above but still have problems with loading the file...
I'm sorry for the spamming, but I just got this:
Error using load
Unable to read file '850\.mat'. No such file or
directory.
So it is calling the number correctly now, using the code I showed above but the '/' is there?
Do not use evalin: if you are a beginner you should learn reliable ways to pass data between workspaces, not slow and buggy methods (like evalin):
This thread exaplins why you should avoid eval, but the same applies to evalin as well:
Solution: just pass arguments. That is why functions have arguments. Passing arguments is neater, faster, more robust, easier to read, ...
Adams answer is incorrect, fullfile will creates this:
'850\.mat'
which shows that fullfile is inserting a file separator between filename and extension. See my answer for the correct code.
Yes, that is true, it was a hasty untested answer off the top of my head based on usual constructs for these kind of situations!

Sign in to comment.

Asked:

on 13 Apr 2017

Commented:

on 13 Apr 2017

Community Treasure Hunt

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

Start Hunting!