problem with input names of vectors loaded in a function

Hi all, i have a problem using the input command. I have a code that calculates mean and std of a lot of vectors saved in the workspace and loaded in my matlab function:
close all
close all hidden
clear all
load('vettori_coppie.mat')
A=(a1prima);
B=(a2prima);
C=(a3prima);
D=(a4prima);
E=(a5prima);
etc..
data=[A B C D E F ...]
Dm_0=mean(data(1:100,:));
Ds_0=std(data(1:100,:));
I have vectors named also, for example, b1prima, b2prima, g5prima, etc... in the workspace, so it changes only the first letter of the vector's name
I want to select the vectors whose i want to calculate mean and std when calling the function in the command window like this:
function_name(a)
or
function_name(d)
etc...
or maybe with the input command.
so the function uses the first letter in loading vectors:
B=('b'2prima);
with b chosen when calling the function or with input command
C=('b'3prima);
D=('b'4prima);
etc..
How can i do this?
(sorry for bad English, I don't know if I explained the problem well enough)

Answers (1)

Do not store your data like this. It is the contents of a variable that should change, not the name itself.
In your case, you can load the mat file in a structure
S = load('vettori_coppie.at')
S is a structure with several fields. You can get the name of the fields using FILENAMES
FN = filenames(S)
To access the contents of a specific field which the user can select you can do something like this:
DesiredField = input('Which variable do you want to process?','s')
if isfield(S, DesiredField)
A = S.(DesiredField) ; % dynamic field name
disp(A)
else
disp(['The variable "' DesiredField '" is not in this file!']) ;
end

Categories

Asked:

on 4 Mar 2016

Edited:

on 4 Mar 2016

Community Treasure Hunt

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

Start Hunting!