0

I am creating a table in a function, so the function outputs a table to the assigned variable name as shown below

[name] = tablefunc(input1, input2)

The thing is I want to be able to have the name be an input that was assigned earlier for example

name = 'dogs'
[something] = tablefunc(input1,input2)

I want to be able to put some code where it says something so that the outputted table for tablefunc is assigned the variable name dogs

It might be confusing why I am doing this but it is because I am extracting tables from a txt file in a for loop so I am getting lots of tables generated and I want to be able to give the tables their appropriate names as opposed to just table1, table2 etc.

0

1 Answer 1

1

That's not a good idea. As an alternative, you should create a structure:

function t = tablefunc(input1,input2)
    t = table(input1,input2);
end

name = 'dogs';
s = struct();
s.(name) = tablefunc(rand(2),rand(2));

You can have one field per txt file.

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

3 Comments

That is very helpful. Can I add a new table to the structure each time I run through the for loop that it is embedded in though?
You are welcome. Of course, e.g. s.('cats') = table()
Never mind I changed the for loop to a while loop and it worked

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.