0

I have a data file dataFile.mat that has two arrays A_numbers and B_numbers.

I want to write a script that takes gets the data from either A_numbers or B_numbers. I do not want to use numbers - I need to keep them as strings.

How can I do the following, where arrayName can be either A or B?

function[] = getData('arrayName')
   importedData = load('dataFile')
   result = importedData.arrayName_numbers
   save 'resultData.mat'

1 Answer 1

1

Assuming you want your function to load all variables saved in dataFile.mat which is in the current working directory, and then save only the variable whose name is stored in the string arrayName "_numbers" into a new file resultsData.mat, the following should do 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 the current working directory in file resultData.mat

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I realized that my MWE was slightly incorrect - the variable under which the data is stored is of the form A_numbers and B_numbers. So I want to use the input which is either A or B to decide whether to look at A_numbers or B_numbers
This only requires a trivial change to the example... you need to concatenate the string "_numbers" as I've done in the updated example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.