Adding multiple variable contents in a single variable

I hope I can explain my problem statement properly.
I have matrix with name from meanSINR1, meanSINR2, meanSINR3, .... meanSINR10. Each matrix is of size 722x2 double values.
I want to put all these matrix values in a single matrix using a for loop.
I tried using sprintf() to move through each matrix name. But I could get the correct result.
tmp1 = zeros(1,2);
tmp2 = [];
for ii = 1:121
tmp1 = sprintf('meanSINR%d',ii);
tmp2 = [tmp2; tmp1];
end
Looking forward to any kind of suggestion. Thank you.
Kindly let me know if anyone can help me in running through multiple file names in python too using a for loop.

1 Comment

"I have matrix with name from meanSINR1, meanSINR2, meanSINR3, .... meanSINR10"
Numbering variable names like that is a sign that you are doing something wrong.
Forcing meta-data into variable names is a sign that you are doing something wrong.
Accessing those variables will force you into writing slow, complex, inefficient, insecure, obfuscated code that is buggy and hard to debug. Read this to know some of the reasons why:
So far you have not told us the most important information: how did you get those variables into the workspace? The place where those variables are created in the workspace would be the best place to fix that very unfortunate data design.

Sign in to comment.

 Accepted Answer

If your data are already stored in dynamically named variables, then I think your only recourse is to use eval to do this task:
% Make up some data smaller than your actual data
meanSINR1 = rand(7,2);
meanSINR2 = rand(7,2);
meanSINR3 = rand(7,2);
meanSINR = [];
for ii = 1:3
eval(sprintf('meanSINR = [meanSINR; meanSINR%d];',ii))
end
It would be much better if you could go further upstream in your code, and use cell arrays or multi-dimensional arrays to store these data. For example, the above could instead have been
meanSINR = rand(7,2,3);
from the beginning, with all the data stored in a single array.

6 Comments

Hi,
Thank you for your answer. So I would like to proceed with this thread on some of my other queries related to the same type of issue. You pointed out correctly that the issue arises from the top of my code. And I would like to address it over there.
I have 20 text file data that I load into matlab in this way.
file1 = readtable('G:\My Drive\20-rack_wood-16m_warehouse-edited\Data\0\RxPacketTrace.txt');
file2 = readtable('G:\My Drive\20-rack_wood-16m_warehouse-edited\Data\0.5\RxPacketTrace.txt');
file3 = readtable('G:\My Drive\20-rack_wood-16m_warehouse-edited\Data\1\RxPacketTrace.txt');
file4 = readtable('G:\My Drive\20-rack_wood-16m_warehouse-edited\Data\1.5\RxPacketTrace.txt');
file5 = readtable('G:\My Drive\20-rack_wood-16m_warehouse-edited\Data\2\RxPacketTrace.txt');
file6 = readtable('G:\My Drive\20-rack_wood-16m_warehouse-edited\Data\2.5\RxPacketTrace.txt');
.
.
.
And so on.
Any sugestion on doing this in few lines of code?
Later, I have to extract only few columns from each file. Which was again a bad practice of coding.
%% 2-Extracting file data
file1_UL = file1(strcmp(file1.DL_UL,'UL'),[1,2,14]);
file2_UL = file2(strcmp(file2.DL_UL,'UL'),[1,2,14]);
file3_UL = file3(strcmp(file3.DL_UL,'UL'),[1,2,14]);
file4_UL = file4(strcmp(file4.DL_UL,'UL'),[1,2,14]);
file5_UL = file5(strcmp(file5.DL_UL,'UL'),[1,2,14]);
.
.
.
And so on.
Kindly suggest some way to shorten such repetitive coding style.
I am sure others may also be facing such problem. It would be a good way to set some example solutions to such problem over here.
Thank You,
Rahul Singh Gulia
You might want to open up a new question for each smaller task that you need to do, starting from the beginning. You are correct that others face similar issues.
For the things you have mentioned, here is some code that might be functional for you, but more importantly you need to understand what each step is doing. I have added comments that should be helpful for that.
Note that at no time have I needed to define names like file1, file2, etc. I did this by using cell arrays. Cell arrays are containers that can hold more complicated objects (e.g. whole tables) in each element. They take a little while to get used to using (e.g. using {} instead of () to access the contents), but are very powerful.
% Get list of files that match a pattern
fileList = dir('G:\My Drive\20-rack_wood-16m_warehouse-edited\Data\*\RxPacketTrace.txt');
% Find the number of files
numberFiles = length(fileList);
% Preallocate cell arrays for the tables and the subset of info
% needed from each table
fileTable = cell(numberFiles,1);
fileUL = cell(numberFiles,1);
% For each file, read the table, and extract the info needed from each table
for nf = 1:numberFiles
fileTable{nf} = readtable(fileList(nf).name);
fileUL{nf} = fileTable{nf}(strcmp(fileTable{nf}.DL_UL,'UL'),[1,2,14]);
end
I like the cell functionality, since it can solve many of my current issues with unncessary long codes.
I just wanted to know if I could see the contents of the RxPacketTrace.txt file that was stored in the 'fileList' cell.
fileList = dir('D:\RahulGulia\25-SINR-heatmaps\Processed_Data\20-rack_wood-16m_warehouse-edited\Data\*\RxPacketTrace.txt');
% Number of files
numberFiles = length(fileList)
% Accessing 1st cell
file1 = fileList(1)
-> Output: struct with fields:
name: 'RxPacketTrace.txt'
folder: 'D:\RahulGulia\iMHS_rahul\NS3_results\JournalPaper-1\Results\25-SINR-heatmaps\Processed_Data\20-rack_wood-16m_warehouse-edited\Data\0'
date: '19-Feb-2022 13:42:22'
bytes: 5105492
isdir: 0
datenum: 7.3857e+05
% Accessing the 'name' entity from the cell
RxPacket = file1.name
-> Output: 'RxPacketTrace.txt'
% But I am not able to get the contects of the file RxPacketTrace.txt
RxPacket_UL = RxPacket.DL_UL
-> Error: Dot indexing is not supported for variables of this type.
So I guess, the cells are created only to one level. And it does not go inside the RxPacketTrace.txt file data.
Solution: I could get the UL_DL column data using the "readcell()" function. But then I have to access each RxPacketTrace.txt file one by one. It does not create a single cell for all my data files.
fileList = readcell("D:\RahulGulia\iMHS_rahul\NS3_results\JournalPaper-1\Results\25-SINR-heatmaps\Processed_Data\20-rack_wood-16m_warehouse-edited\Data\*\RxPacketTrace.txt");
This line gives error saying "Unable to find or open".
Please sugegst if I can use any other function to solve the issue.
"I just wanted to know if I could see the contents of the RxPacketTrace.txt file that was stored in the 'fileList' cell."
No file content was stored in that variable, because DIR does not import file data. DIR just asks the OS for names of files/folders in some location. Nothing more, nothing less.
FILELIST is not a cell array, it is a structure array:
"But I am not able to get the contects of the file RxPacketTrace.txt"
The character vector stored in the NAME field is simple text of the filename:
  • a simple piece of text cannot contain your file data,
  • so far there is nothing in your code that imports file data.
Getting the name of a file is not the same as importing file data.
"Please sugegst if I can use any other function to solve the issue. "
the cyclist already showed you one good function for importing data from your data files: READTABLE. Unfortunately the cyclist's code does not take into account the different folders, most likely an oversight.
Note that you could avoid the cell arrays by using the already defined structure array:
P = 'G:\My Drive\20-rack_wood-16m_warehouse-edited\Data';
S = dir(fullfile(P,'*','RxPacketTrace.txt'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
S(k).data = readtable(F);
end
All of the file data will be in the structure S. For example, the second file:
S(2).folder
S(2).name
S(2).data
Thanks for fixing my oversight, @Stephen23!
I really appreciate to hear back from @the cyclist and @Stephen23 on my issues. Thank you for your time and patience to help resolve my issue.
I will surely learn to do efficient coding in MATLAB, and hope someone else also learns from my mistakes.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!