Dynamic variables in loop

5 次查看(过去 30 天)
J S
J S 2018-7-10
编辑: jonas 2018-7-10
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
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.
J S
J S 2018-7-10
Like this repeating more or less with about 40000 lines. The distance between reoccurring strings is variable so the index cannot be simple vector:
'in'
'chips'
''
'Seat'
'2:'
'Selimeli2'
''
'1500'
'in'
'chips'
''
'Seat'
'3:'
'SOSSSSS'
''
'1500'
'in'
'chips'
''
'Seat'

请先登录,再进行评论。

采纳的回答

jonas
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]

更多回答(2 个)

J S
J S 2018-7-10
编辑:J S 2018-7-10
%%Import data from text file.
% Script for importing data from text file
%%Initialize variables.
filename = '/Users/juliushannesschonleber/Documents/POK/HH20170417_T1886468393.txt';
delimiter = ',#()[] ''-';
%%Format for each line of text:
% column1: text (%q)
% column2: text (%q)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s';
%%Open the text file.
fileID = fopen(filename,'r','n','UTF-8');
% Skip the BOM (Byte Order Mark).
fseek(fileID, 3, 'bof');
%%Read columns of data according to the format.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
text = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'EmptyValue' ,NaN, 'ReturnOnError', false);
%%Close the text file.
fclose(fileID);
%%Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans;
text= text{1,1};
% Index_t = strmatch('Tournament', text);
% Index_h = strmatch('Hand', text);
ind_playercount = strmatch('max', text)-1;
playercount= str2double(text(ind_playercount,1));
playercount= playercount(1,1);
for i=1:playercount
index_chips.playercount = 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

Steven Lord
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 个评论
J S
J S 2018-7-10
Thanks but thats not what I need. The chips for each player vary from play to play and I want to create a cell array for each player with the amount of chips they have at the end of each hand played
Steven Lord
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 CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by