MATLAB - Passing a string to a function and loading appropriate data file -


i have data file datafile.mat has 2 arrays a_numbers , b_numbers.

i want write script takes gets data either a_numbers or b_numbers. not want use numbers - need keep them strings.

how can following, arrayname can either a or b?

function[] = getdata('arrayname')    importeddata = load('datafile')    result = importeddata.arrayname_numbers    save 'resultdata.mat' 

assuming want function load variables saved in datafile.mat in current working directory, , save variable name stored in string arrayname "_numbers" new file resultsdata.mat, following should it:

function [] = getdata( arrayname )  load('datafile'); save('resultdata.mat', [arrayname '_numbers']);  end 

testing it:

a_numbers = ones(3,1); b_numbers = ones(3,1).*42; save('datafile.mat');  getdata('b'); 

this should result in array b_numbers being stored in current working directory in file resultdata.mat


Comments