- Instead of using start_indexEMG and end_indexEMG, you could say index = [yourStartIndex : yourEndIndex]. Then use RSOL_ROI = RSOL(index) for example.
- use clear to programatically delete variables when you're done with them to keep your workspace clean.
- You "still want them stored somewhere". Well that "somewhere" is the workspace. You could, if it's appropriate to your application, save varaibles to a .MAT file, then load that file into your workspace when it's needed.
- It is not recommended to generate individual variables in a loop.
- To call a field in a structure use the structure variable name, such as A.RSOL. You can't just call RSOL and expect to get the field within the A structure.
- To generate a structure in a loop would be something like this:
General variable creation question
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I am still new to programming in Matlab and i tend to code in a way that generates a lot of variables in a workspace. Generally it's fine but i would like to know what my other options are to minimize workspace clutter. For an example, i am extracitng a region of interest (ROI) from some EMG data using the same start and end index, so my code snippet looks like this:
RSOL_ROI = RSOL(start_indexEMG:end_indexEMG);
RMG_ROI = RMG(start_indexEMG:end_indexEMG);
RTA_ROI = RTA(start_indexEMG:end_indexEMG);
RMH_ROI = RMH(start_indexEMG:end_indexEMG);
RVL_ROI = RVL(start_indexEMG:end_indexEMG);
RRF_ROI = RRF(start_indexEMG:end_indexEMG);
I plan to plot the _ROI variables later so i still want them stored somewhere where i can call them later. Can someone suggest a way to write code that generates the 6 _ROI variables but doesn't store them in workspace? And just for learining purposes can someone write a for loop for this as well? I have tried a few times and did not seem to work.
EDIT: I know i can place the variables into a struct. As a quick question within this question what if i have a struct with the following 6 variables stored: RSOL,RMG,RTA,RMH,RVL,and RRF. And the struct is named A. Is there a way to call the variables by direct name in my script. Basically something like X=2*RSOL. instead of having to say X=2*A.RSOL. Hope that makes sense!
Thank you for your help in advance!
0 个评论
采纳的回答
Joe Vinciguerra
2023-3-22
names = {'RSOL_ROI'
'RMG_ROI'
'RTA_ROI'
'RMH_ROI'
'RVL_ROI'
'RRF_ROI'}
A = struct()
for i = 1:length(names)
A.(names{i}) = [];
end
更多回答(1 个)
John D'Errico
2023-3-22
Don't generate a zillion variables. Instead, learn to use arrays. Cell arrays, structs, multi-dimensional arrays. This will leave you with ONE variable to chase around.
It is just a habit you need to learn as good programming practice.
3 个评论
Stephen23
2023-3-24
One important step is to understand that meta-data (like the categories RSOL, RMG, RTA, RMH, RVL, and RRF) are data. And data belong inside variables, not in the code itself. Once you do that, then using arrays to store and process your data will be easier... and your code will be simpler and more generalizable as well.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!