Dynamic variables in loop
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
jonas
le 10 Juil 2018
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.
Réponse acceptée
jonas
le 10 Juil 2018
Modifié(e) : jonas
le 10 Juil 2018
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 commentaires
Plus de réponses (2)
Steven Lord
le 10 Juil 2018
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 commentaires
Steven Lord
le 10 Juil 2018
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".
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!