Dynamic variables in loop
2 次查看(过去 30 天)
显示 更早的评论
Hey there,
I got some string data and want to plot the amount of chips of N amount of players (<15).
I tried dynamic variables (I know they shouldn't really be used) and similar methods but cannot get it to work after quite some time spend on it.
Basically I want to store all the data with sth like 'index_chips.playercount' and 'chips.playercount' instead of computing it manually with 'index_chips1' 'index_chips2' 'index_chips3' and so on.
for i=1:playercount %N amount of players
index_chips1 = strmatch('1:', text)+4;
chips1= str2double(text(index_chips1,1));
chips1= num2cell(chips1);
chips1(cellfun(@(chips1) any(isnan(chips1)),chips1)) = [];
chips1=cell2mat(chips1);
plot(chips1)
hold on
end
This term (gives me loads of trouble):
'1:'
also needs to be instead (the colon needs to be after the playercount)
'playercount:'
Thanks a lot for any suggestions
2 个评论
jonas
2018-7-10
Can you give some background? What does your data look like? Can you share it? Im sure there is a simple solution that does not involve dynamic variables.
采纳的回答
jonas
2018-7-10
编辑:jonas
2018-7-10
Here is something I stitched together to plot the chipcounts. Perhaps not the most efficient code, but no need for dynamic variables. I used dynamic field names as suggested by Steven Lord.
%%obtain data from txt using Reglar Expressions
text = fileread('poker.txt');
exp='\w*\s\W\d+ in chips)';
[a,b]=regexp(text,exp);
for i=1:length(a)
str=text(a(i):b(i)-10);
data{i}=strsplit(str,' (');
end
%%find and store names
for i=1:9
names{i}=data{i}(1)
end
names=[names{:}];
data2 = [data{:}];
%%Manipulate data and plot
for i=1:9
name=names{i}
[inds]=strcmp(data2,name);
chipcnt=data2([false inds(1:end-1)]);
chipcnt=cellfun(@str2num,chipcnt)
plot(chipcnt)
myStruct.(names{i}) = chipcnt
end
myStruct =
struct with fields:
D4ns3n: [1×60 double]
Selimeli2: [1×53 double]
SOSSSSS: [1×43 double]
ShaveBOST: [1×145 double]
Rossoneri03: [1500 1497 1494 1471]
art: [1×37 double]
snipper66: [1×87 double]
Competency: [1×81 double]
ID: [1×145 double]
0 个评论
更多回答(2 个)
Steven Lord
2018-7-10
Don't create variables named like that.
numPlayers = 10;
chipCounts = randi([1 100], 1, numPlayers)
Now when you want the chip count for player 7 (for example) it's easy.
chipCounts(7)
Plotting chip counts? Also easy.
figure
subplot(3, 1, 1)
plot(chipCounts)
title('Line plot');
subplot(3, 1, 2);
bar(chipCounts)
title('Bar chart');
subplot(3, 1, 3)
pie(chipCounts)
title('Anyone for pie?');
2 个评论
Steven Lord
2018-7-10
Don't create one variable per player. I would create a struct with one field per player and store vectors in those fields (you can use dynamic field names to refer to the field if you have a char vector containing the name of the field) or a table array with one table variable per player.
Creating variables with dynamic names can be slow and can lead to Bobby Tables syndrome or "poofing".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!