Convert vectors with different names to matrix

Hi guys,
I have n vectors (1x4000) whose names are a1, a2 up to an. I would like to create a matrix which is the rsult of vertcat(a1,a2,...,an). How can I do it without specifying the names of all the vectors in vertcat?
Thank you!

7 Kommentare

Rik
Rik am 2 Nov. 2017
You don't. You should restructure your data at the point it gets into those variables. Using numbered variables is red flag, because you will be forced to use eval to deal with them.
Stephen Cobeldick is very pasionate about this topic (and with good reason), so I have taken the liberty of adapting some of his thoughts on this topic: Introspective programing (eval and other functions like it) is slow, and the MATLAB documentation warns specifically against it. Using eval is slow, makes debugging difficult, and removes all code helper tools (code checking, syntax hints, code completion, variable highlighting, etc.). A longer discussion can be found here. If you are not an expert and you think you need eval, there are probably better solutions to your problem.
Adam
Adam am 2 Nov. 2017
If you are picking up someone else's workspace that already has these variables in then you may have to use some ugly code at the interface between that and your code to get them into a matrix. The fact that you want them in a matrix is at least a sign of going in the right direction rather than people asking to do the reverse.
However, as Rik points out, the best solution, if you have full control over all the code, is to never have those individual variables created in the first place.
Stephen23
Stephen23 am 2 Nov. 2017
Bearbeitet: Stephen23 am 2 Nov. 2017
@salvor: the best solution is to fix the problem at the source, not to try and create some hack fix later. Fixing the source means not generating those variables in the first place. It is easy to do this when defining values in a loop or loading data from files, so you should not have any difficulties in doing this.
If you tell us how those variables are created then we will show you better, neater, more reliable, and more efficient ways to write that code.
salvor
salvor am 2 Nov. 2017
Thank you for your answers guys. Unfortunately they come from an external workspace and I don't have control on it.
Stephen23
Stephen23 am 2 Nov. 2017
Bearbeitet: Stephen23 am 2 Nov. 2017
@salvor: what exactly does "Unfortunately they come from an external workspace" mean ?
Whether importing data or passing data as arguments, what names have in some other workspace is totally irrelevant. For example, when loading a .mat file you should simply load into an output argument (which happens to be a structure): you do not have to use the same variable names as when they were saved.
If you actually tell us how this data is generated then we can help you to writer better code. Mot importantly: how are you getting these variables into your function/base workspace?
salvor
salvor am 2 Nov. 2017
Bearbeitet: salvor am 2 Nov. 2017
It means that I have been given a set of 10 different vectors, each of them 1x4000 called a1, a2 up to a10. I want to create a matrix 10x4000 containing these 10 vectors.
Stephen23
Stephen23 am 2 Nov. 2017
Bearbeitet: Stephen23 am 2 Nov. 2017
@salvor: I asked how you are getting this data into your workspace: are you loading the data from a file, or running some function or script, or doing something else?
Data does not just magically appear in a workspace. Some operation has to put it there, like loading from a file. Please tell us how you get that data into your workspace. Then we can help you to write better code.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

OCDER
OCDER am 2 Nov. 2017
You could use the workaround of save/load to get what you want.
%Suppose you have these a1, a2 ... an
a1 = rand(1, 4000);
a2 = rand(1, 4000);
a3 = rand(1, 4000);
a13 = ones(1, 4000);
save('TEMPFILE.mat', '-regexp', 'a\d+') %saves variables with names a[number]
a = load('TEMPFILE.mat'); %loads into structure, a.a1, a.a2, a.a3, ...
delete('TEMPFILE.mat'); %delete temp mat file
anums = str2double(strrep(fieldnames(a), 'a', '')); %get the nth number of a
[~, sortidx] = sort(anums); %get the sort order. 1, 10, 2, 3 --> 1, 2, 3, 10
a = struct2cell(a); %convert structure to cell
a = vertcat(a{sortidx}); %sort a in right order, and then combine vertically

1 Kommentar

Stephen23
Stephen23 am 2 Nov. 2017
Bearbeitet: Stephen23 am 2 Nov. 2017
Note: sorting into the correct order is simple with my FEX submission natsort.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Gefragt:

am 2 Nov. 2017

Bearbeitet:

am 2 Nov. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by