So on my end state I want to have the user input their initials. I have setup an array that has all the alphabet, triggers to select a letter and font_draw, but I cant seem but I get an error saying my function is not defined. Any ideas?
userInitials: function(){
io_clear();
a = 0
l[0] = " "
l[1] = "a"
l[2] = 'b'
l[3] = 'c'
l[4] = 'd'
l[5] = 'e'
l[6] = 'f'
l[7] = 'g'
l[8] = 'h'
l[9] = 'i'
l[10] = 'j'
l[11] = 'k'
l[12] = 'l'
l[13] = 'm'
l[14] = 'n'
l[15] = 'o'
l[16] = 'p'
l[17] = 'q'
l[18] = 'r'
l[19] = 's'
l[20] = 't'
l[21] = 'u'
l[22] = 'v'
l[23] = 'w'
l[24] = 'x'
l[25] = 'y'
l[26] = 'z'
str = ""
if(ig.input.pressed('up')){
if (a>26){
a+= 1;
}else{
a = 0;
}
}
if(ig.input.pressed('down')){
if (a<0){
a -= 1;
}else{
a = 26;
}
}
this.font_draw(x,y,str+'['+1[a]+'] ');
if(ig.input.pressed('space')){
str += l[a];
}
},
draw:function(){
if(this.gameOver){
this.font.draw(userInitials, ig.system.width/2, 95, ig.Font.ALIGN.CENTER);
}
When you're calling functions you want to use 'this'.
If there's no current object, 'this' refers to the global object. In a web browser, that’s ‘window’ — the top-level object, which represents the document, location, history and a few other useful properties and methods.
‘this’ remains the global object if you’re calling a function:
So therefore, this.userInitials() will work:
this.font.draw(this.userInitials(), ig.system.width/2, 95, ig.Font.ALIGN.CENTER);
Related
I have a Matlab function. I need to generalize this function. This code’s aim is to check this IndicMPs are in the TableTemp, if it is there, then we extract relevant age limits, such as: Age_Limite_DC, Age_Limite_IT, Age_Limite_Ch and Transfert_Prime_IT_DC. My idea is to generalize, passing parameters to find out the "Type_pret" is.(May be I'm wrong) Since I'm beginner to Matlab can someone help me to encode a more generic function that can be used in a more general context?
function Structure = optimisation_function()
Data = load('Data.mat');
Structure = Data.Structure;
TableTemp = Data.TableTemp;
Age_Limite_DC = zeros(size(Structure,1),1);
Age_Limite_IT = zeros(size(Structure,1),1);
Age_Limite_CH = zeros(size(Structure,1),1);
Transfert_Prime_IT_DC = zeros(size(Structure,1),1);
for IndexMPAL = 1 : length(Structure.AnneeSouscription)
% Determine Type_Pret (Loan Type)
if ~isempty(strfind(Structure.Type_Pret{IndexMPAL},'A'))
Type_Pret = 'A';
elseif ~isempty(strfind(Structure.Type_Pret{IndexMPAL},'B'))
Type_Pret = 'B';
elseif ~isempty(strfind(Structure.Type_Pret{IndexMPAL},'C'))
Type_Pret = 'C';
elseif ~isempty(strfind(Structure.Type_Pret{IndexMPAL},'D'))
Type_Pret = 'D';
elseif ~isempty(strfind(Structure.Type_Pret{IndexMPAL},'E'))
Type_Pret = 'E';
end
MP_CP = Structure.NomCodeProduit(IndexMPAL);
MP_AnSous = Structure.AnneeSouscription(IndexMPAL);
MP_TypePret = Type_Pret;
IndicCP = strcmp(MP_CP, TableTemp.CodeProduit);
IndicAS = MP_AnSous== TableTemp.AnneeSouscription;
IndicTP = strcmp(MP_TypePret, TableTemp.TypePret);
IndicMP = IndicCP & IndicAS & IndicTP;
if ~any(IndicMP)
Msg = strcat('CodeProduct:',MP_CP{1}, ', Année Souscription:', num2str(MP_AnSous), ', Type Prêt:', MP_TypePret);
error('Error', Msg)
else
Age_Limite_DC(IndexMPAL,1) = TableTemp.Age_Limite_DC(IndicMP,1);
Age_Limite_IT(IndexMPAL,1) = TableTemp.Age_Limite_IT(IndicMP,1);
Age_Limite_CH(IndexMPAL,1) = TableTemp.Age_Limite_CH(IndicMP,1);
Transfert_Prime_IT_DC(IndexMPAL,1)=
TableTemp.Transfert_Prime_IT_DC(IndicMP,1);
end
end
Structure.Age_Limite_DC = Age_Limite_DC;
Structure.Age_Limite_IT = Age_Limite_IT;
Structure.Age_Limite_CH = Age_Limite_CH;
Structure.Transfert_Prime_IT_DC = Transfert_Prime_IT_DC;
end
The if/elseif can be simplified with a cell array:
liststr = {'A','BB','C','D','E'}; % builds a cell array, each cell contains a string
Positive_matches = strfind(liststr,Structure.Type_Pret{IndexMPAL}) % returns a list for each cell of the indices where the element was found (empty if none)
Index = find(~cellfun('isempty', Positive_matches )) % converts the previous list into a logical 0/1 array indicating whether an item was found (1) or not (0)
% if isempty(Index); continue; end % If no index is found, to avoid an error in the next instruction, skips the rest of the code.
Type_Pret = liststr(Index(1));
If the Type_Pret are the same in your list and in Structure? , you can usestrcmp`:
liststr = {'A','B','C','D','E'};
if any(strcmp(Structure.Type_Pret,liststr))
Type_Pret = Structure.Type_Pret
else
% handle error
end
You can also work directly on Structure.Age_Limite_DC without using Age_Limite_DC.
i have a matlab function for listing all files in a folder, including its children folders. i can display them all , but i have no idea about how to store them in my output variable .
function out = searchfolder(varargin)
if nargin<1
helpdlg('no input floder');
return;
else
curfolder=varargin{1};
end
if ~isdir(curfolder) || length(dir(curfolder)) < 3
return;
end
children = dir(curfolder);
%eliminate '.' and '..'
children = children(3:end);
allfiles = {};
for i=1:length(children)
child = children(i);
thisone = fullfile(curfolder,child.name);
if child.isdir
searchfolder(thisone);
else
disp(thisone);
end
end
so how to figure out this problem?
with excaza's help , i have the code :
function out = searchfolder(varargin)
out = {};
if nargin<1
% helpdlg('no input floder');
return;
else
curfolder=varargin{1};
end
if ~isdir(curfolder) || length(dir(curfolder)) < 3
return;
end
children = dir(curfolder);
%eliminate '.' and '..'
children = children(3:end);
allfiles = {};
for i=1:length(children)
child = children(i);
thisone = fullfile(curfolder,child.name);
if child.isdir
out = [ out ;searchfolder(thisone)];
else
disp(thisone);
out = [out;{thisone}];
end
end
How can I read different text files in Matlab.
Considering that there are 33 txt files and that every one should be processed.
Here is part of my code with the error.
textFilename = cell(1,33);
id = cell(1,33);
for k=1:33;
textFilename{k} = fullfile('C:\Users\Desktop\SentimentCode\textfiles',['file' num2str(k) '.txt']);
id{k} = fopen(textFilename{k},'rt');
str{k} = textscan(id{k},'%s%s');
str(str == '.') = '';
str(str == '_') = '';
str(str == '-') = '';
% Remove numbers from text
T =regexprep(str, '[\d]', ' ');
end
and my error is:
??? Undefined function or method 'eq' for input arguments of type 'cell'.
Error in ==> Untitled9 at 23
str(str == '.') = '';
I would like to do something similar to the following, except I would only like to remove 'g' and'g' because they are the duplicates that occur one after each other. I would also like to keep the sequence the same.
Any help would be appreciated!!!
I have this cell array in MATLAB:
y = { 'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h'}
ans =
'd' 'f' 'a' 'w' 'a' 'h'
There was an error in my first answer (below) when used on multiple duplicates (thanks grantnz). Here's an updated version:
>> y = { 'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h' 'h' 'i' 'i' 'j'};
>> i = find(diff(char(y)) == 0);
>> y([i; i+1]) = []
y =
'd' 'f' 'a' 'w' 'a' 'j'
OLD ANSWER
If your "cell vector" always contains only single character elements you can do the following:
>> y = { 'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h'}
y =
'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h'
>> y(find(diff(char(y)) == 0) + [0 1]) = []
y =
'd' 'f' 'a' 'w' 'a' 'h'
Look at it like this: you want to keep an element if and only if either (1) it's the first element or (2) its predecessor is different from it and either (3) it's the last element or (4) its successor is different from it. So:
y([true ~strcmp(y(1:(end-1)),y(2:end))] & [~strcmp(y(1:(end-1)),y(2:end)) true])
or, perhaps better,
different = ~strcmp(y(1:(end-1)),y(2:end));
result = y([true different] & [different true]);
This should work:
y([ diff([y{:}]) ~= 0 true])
or slightly more compactly
y(diff([y{:}]) == 0) = []
Correction : The above wont remove both the duplicates
ind = diff([y{:}]) == 0;
y([ind 0] | [0 ind]) = []
BTW, this works even if there are multiple duplicate sequences
eg,
y = { 'd' 'f' 'a' 'g' 'g' 'w' 'a' 'h' 'h'};
ind = diff([y{:}]) == 0;
y([ind 0] | [0 ind]) = []
y =
'd' 'f' 'a' 'w' 'a'
So lets say my calculation is this
Let (
[
$result[1] ="1";
$result[2] ="!";
$result[3] = "3";
$result[4] = "4";
$result[5] = "5";
$result[6] = "6";
$result[7] = "7";
$result[8] = "8";
$result[9] = "-";
$result[10] = "10";
$result[11] = "11";
$result[12] = "12";
$result[13] = "13";
$result[14] = "14";
$result[15] = "15";
$result[16] = "!";
];
$result[1] &
$result[2] &
$result[3] &
$result[4] &
$result[5] &
$result[6] &
$result[7] &
$result[8] &
$result[9] &
$result[10] &
$result[11] &
$result[12] &
$result[13] &
$result[14] &
$result[15] &
$result[16]
)
this is pretty straight forward I make and array and then I want to it to return the array as a string is there and easier way to concatenate the the values of the result array ?
** Sample custom functions ***
trying to use #chuck 's code not sure what I'm doing wrong I could figure out how to upload a file so here are some mimages
Here's two custom functions that should work if you can assume that once you get to a blank value in the array, you've reached the end of the array. I wrote it very quickly and only tested it once, but perhaps it'll do the job for you.
ConcatArray( VarName ) = _ConcatArray( VarName; 1 )
This just calls a recursive function with an initial value.
_ConcatArray( VarName; Iteration ) = Let(
[
Var = Evaluate( "$" & VarName & "[" & Iteration & "]" )
];
Case(
IsEmpty( Var );
"";
Var & _ConcatArray( VarName; Iteration + 1 )
)
)
I then opened the Data Viewer in FileMaker and tested it with this calculation.
Let (
[
$result[1] ="1";
$result[2] ="!";
$result[3] = "3";
$result[4] = "4";
$result[5] = "5";
$result[6] = "6";
$result[7] = "7";
$result[8] = "8";
$result[9] = "-";
$result[10] = "10";
$result[11] = "11";
$result[12] = "12";
$result[13] = "13";
$result[14] = "14";
$result[15] = "15";
$result[16] = "!"
];
ConcatArray( "result" )
)
The result was 1!345678-101112131415!.
You could do it with a recursive custom function. Specify the end range and the var name as parameters and it could then repeatedly call itself with an incrementing index number until that index number equaled the end range.