Today I am creating a character parser to pass data from one GUI to another for a loading function and then vice versa for the save function.
Problems:
1) How do I select the active value in a list box? Ex: If user selects 'type 1', how do I capture this? Value of hObject = a number and String of hObject = array. I want the selected element of an array.
1] String(value)
I added the following lines (193-198) to function type_pushbutton.
typestring=get(handles.type_popupmenu,'string');
typename=(typestring(value));
hmodel=getappdata(0,'hModel');
data_type=getappdata(hmodel,'data_type');
typenumber=strmatch(typename,data_type(:,1));
typehouse(value,handles.filename,typenumber);
Date: Tuesday, Week 1, 10/1/13
Problems:
1) Error using set
"Conversion to double from cell is not possible."
set(data_type(handles.typenumber,:) , {tempdata,object_list,parameter});
1] Don't use set to overwrite a row in an array, just use equal notation!
data_type(handles.typenumber,:)={tempdata,object_list,parameter};
2) Error using strmatch
"Error using cell/strmatch (line 20)
Requires character array or cell array of strings as inputs."
data_type=getappdata(hmodel,'data_type');
typenumber=strmatch(typename,data_type(:,1));
This means that strmatch is not receiving correct input.
I investigate and find out 'Type 1' is missing. DUN DUN DUN!
K>> data_type(:,1)
ans =
{1x1 cell}
'type 2'
This means that when I edited the text file and saved it, it didn't save properly even though it didn't throw up any error messages.
What a twist!
This problem arose from me using strcat to combine 'Type ' and "num2str(handles.typenumber)".
Strcat cuts off spaces in strings so I changed 'Type ' to {'Type '} to quickly solve that issue.
However, it backfired on me because the data type changed from string to cell!
I feel like a G now!
The answer to my problem is: how do I combine two strings, one which has a space, into a single string?
Looking at previous code, I just use a freaking equal sign and ['s!
"new_type_name=['Type ',num2str(typesize)];"
Blamo, it works!
Peace out, y'all.
Date: Wednesday, Week 1, 10/2/13
1) When I press "add" in model GUI for a new house type, I get this:
"Index exceeds matrix dimensions."
Error in model>type_pushbutton_Callback (line 197)
typenumber=strmatch(typename,data_type(:,1));
Reason: Input is empty - "datat_type" = 0x0 double when debugging
data_type=getappdata(hmodel,'data_type');
Solution: Since data_type does not exist, that means typenumber does not exist. I need a value for typenumber or else I cannot pass it onto the typehouse GUI.
if(isempty(data_type))
typenumber==0;
else
typenumber=strmatch(typename,data_type(:,1));
2) When I press save in the typehouse GUI from the new house type, I get this:
Error using cat
Dimensions of matrices being concatenated are not consistent.
types=cat(1,types,new_type_name);
types consists of 'new' and in this case, we're trying to add 'Type 1', which is longer than new.
The cat function will not let us add two matrices of different dimensions.
I want to input the result of 'new' and 'Type 1' back into the listbox, which accepts a bunch of strings. Or does it? Maybe it will accept a cell array?
Solution: Make 'new' into a cell so that 'Type 1' and this cell will be the same size? Why is that?
if(~iscell(types))
types={types};
end
3) When I press save on what should be a 'Type 2' in typehouse GUI (new and Type 1 already exist)
Error: Subscripted assignment dimension mismatch.
data_type(handles.typenumber,:)={tempdata,object_list,parameter};
Reason: Typenumber doesn't exist (again). Debugging console wise leads to
K>> handles.typenumber
ans =
[]
DUN DUN DUN!
The problem stems from the code I wrote earlier (oops)
if(isempty(data_type))
typenumber=0;
else
typenumber=strmatch(typename,data_type(:,1));
end
Data type is not empty but ends up with typenumber as an empty cell.
Typenumber is empty because typename is 'new' and this does not match anything in data type.
I'm getting lost in the multiple steps so I'm going to state what I want:
Irvin's desirable outcome: Typenumber = 2
I decided to be lame and used an "if is empty" statement so that if typenumber is empty, it would simply make it the size of the list +1. In this case, it becomes 2. LOL
4) Boy the debugging never stops huh?
After I get the Typenumber=2 working, it's not showing up on the model GUI.
There should be a type 2 but I only see 'new' and 'Type 1'. DAMN YOU!
Further investigation leads to this:
data_type =
'Type 1' {1x1 cell} {1x3 cell}
'Type 2' 'house' {1x3 cell}
'Type 3' 'house' {1x3 cell}
'Type 4' 'house' {1x3 cell
The list is actually correct! Butt it's not displaying...how strange.
Problem: How do I get model GUI to update its list box when I close typehouse GUI?
The callbackfunction!
% --- Executes on selection change in type_popupmenu.
function type_popupmenu_Callback(hObject, eventdata, handles)
Uh hell yes!
Before
% type_popupmenu=getappdata(hmodel,'type_popupmenu');
% types=get(handles.type_popupmenu,'String');
After
data_type=getappdata(hmodel,'data_type');
set(handles.type_popupmenu,'String',{'new',data_type{:,1}});
Instead of relying on an old list called type_popupmenu, I switched to the more recent one, data_type.
When I put in set(X,'string',data_type{:,1}) it gave me Error using set
Invalid parameter/value pair arguments
I went back and checked data_type{:,1}, which is the first column, and realized it was spitting out two answers. I put them in a cell (as the solution to most of my problems are) and lo and behold, it works! The 'new' was taken out but I just pulled a lamer and added it in manually, as you can see above.
Thus concludes my adventure today of debugging model -> typehouse GUI problem.
Irvin: 1
Gridmat: 9999999
Yeah, I'll finish it one day. One day.
Date: Monday, Week 4, 10/20/13
Goal: Get selector's save to text function and pull data from one part of GUI to another function done by Wednesday, October 23.
Deliverable: GUI that will produce the following in text file:
[Selector Data]
646,A, 7, 25, 1, 15, 2, 20
Steps to implement
1) Pull data from one part of GUI to another - type_uitable to select_uitable
1,15
2,20
needs to be come
1,15,2,20
or
A
B
needs to be
A,B
2) Take select_uitable data and save to text file.
3) Add load function to opening fcn of selector so that it can read saved files
4) ???
5) Profit
Errata List
1) 4x2 ghost data still in table despite deleting from fig file
types=get(handles.type_uitable,'Data');
data=cat(1,data,{node{:},phase{:},transnum,transrate,types});
K>> data
data =
'' '' '' '' ''
'' '' '' '' ''
'' '' '' '' ''
'' '' '' '' ''
data SHOULD be 1,15,2,20 (based on deliverable remember?)
Yeah, I'll finish it one day. One day.
Date: Friday, Week 4, 10/25/13
Goal unmet from last time: Get selector's save to text function and pull data from one part of GUI to another function done by Saturday, October 26.
Deliverable: Same as before.
Note: It is hard to work as efficiently as before since I don't remember the inputs and outputs. If I make a paper chart of the inputs and outputs, I have to remember to take it with me wherever I go.
1) Error using fgets
Invalid file identifier. Use fopen to generate a valid file identifier.
1] I traced the errors line by line until I came to the shocking conclusion that the GUI was referencing a file that wasn't there. D'oh! Lame lame lame error. -_-
2) 187 data=cat(1,data,{node{:},phase{:},transnum,transrate,{types}});
Error using cat
Dimensions of matrices being concatenated are not consistent.
Now I need to figure out how to change 5x2 cell into a single string to display.
Or: pull data from a structure into a string to display.
I'll come back to this...I swear. :/
Date: Saturday, Week 4, 10/26/13
Goal unmet from last time: Get selector's save to text function and pull data from one part of GUI to another function done by today.
Deliverable: Same as before.
Y x 2 cell array needs to be converted into a single string.
Ding, code is done, code is done!
types_string='';
[p q]=size(types);
for y=1:p
for x=1:q
types_string=[types_string,types{y,x},','];
end
end
What is pre allocation? MATLAB says I need to assign memory to my variable and then set it to 0 after so that it doesn't have to keep adding memory? I feel so n00b in programming...
Next step: add save button and save to GUI data: data_selector
1) Adding save button from tooltip does not work. Have to create a button and call it save.
function psave_Callback(hObject, eventdata, handles)
% hObject handle to psave (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selector_data=get(handles.select_uitable,'Data')
hmodel=getappdata(0,'hModel')
setappdata(hmodel,'selector_data',selector_data);
Verify if selector data is what we want:
343 selector_data=get(handles.select_uitable,'Data')
selector_data =
'633' 'A' [7] [35000] [1x44 char]
'633' 'A' [7] [35000] [1x44 char]
'633' 'A' [3] [15000] [1x44 char]
Hell yes!
2) add lines in model.m to get data from GUI data and print to .m text file
get data from GUI data = break down cell into lines? and store them line by line? How did I do this for typehouse?
In type house, Fred used 3 for loops to read a Y x 3 cell array.
This means I will need 5 for loops to read a Y x 5 cell array.
I wonder if there is a better way to do this than 5 for loops.
How do you access a cell within a cell?
cell{X,Y}{X,Y}
The save to text function rewrites all the data over again! It doesn't scan for existing data and then adds in the new stuff, it adds everything!
Just mimic typehouse because they are both cell arrays.
See Errata List #1
Step after that: Add read function in model.m to get data from text file to GUI data: data_selector
Dance because I am awesome and have finished basic debugging/"programming" for MATLAB and am now ready to go into Facebook as an 1337 h4ck3r.
/Sarcasm
Errata List
1) Not able to access entire row in data_selector due to varying data types
example: I want to print the first row of data_selector, a cell array.
It SHOULD be: A, B, C
but it REALLY is: C
1] I made data_selector into a cell
data_selector =
'633' 'A' [7] [35000] [1x44 char]
'633' 'A' [7] [35000] [1x44 char]
'633' 'A' [3] [15000] [1x44 char]
data_selector{1,:}= '633' 'A' [7] [35000] [1x44 char] GOOD
data_selector{1,:}= [1x44 char] BAD
2) Incorrect use of fprintf
fprintf(stuff) = WRONG
fprintf(fid, %d = int, %s = string, %f= floating) RIGHT
3) Incorrect storage of data
Loading the data from the text file:
[Selector Data]
632,A, , ,new,,Type 1,59, (line A)
632,A, , ,new,,Type 1,59,
Line A is stored as 1x1 cell, which IS NOT RIGHT.
It SHOULD be stored as a 1x5 cell.
ALSO: new should be taken out.
I'm stuck on what to do with this cell situation.
Is there any way I can write to the text file as a cell array so that reading it will result in...a cell array?
I do NOT want to do the typehouse approach...
I guess I could since I know where every column is going...
As in...IF i see 6XX, then assign every line after that to a new column.
The condition would be "pos=strfind(tline,'6')"
Hmmm...that could work. Should I keep working past my midnight curfew? Yeah let's do it! I slept more than enough today anyways.
Date: Sunday, Week 4, 10/27/13
Finish your shit from before.
INPUT: (X x 5 array)
'633' 'A' [3] [15000] [1x44 char]
OUTPUT:
633
A
3
15000
Type 1, X, Type 2, Y
SOLUTION:
for x=1: size(array,1)
for y=1: size(array,2)
fprintf(array{x,y})
end
end
Then:
INPUT:
633
A
3
15000
Type 1, X, Type 2, Y
OUTPUT:
X x 5 array
'633' 'A' [3] [15000] [1x44 char]
if ([House Type])
array={5,y}
Should I set the array size?
while (~isempty(pos=strfind(tline,'6')))
array{1,1}=tline
tline=fgetl(fid)
array{1,2}=tline
etc until
array{1,5}=tline
THEN LOOP BACK TO 6 condition
setappdata(hmodel,'selector_data',selector_data)
selector_data needs to be X x 5 cell
Errata List
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2bAAk1tljOvWPFlFpKGCF2Y8bwIHwmVBknJabeAUiwmjp4v0SC3JfSrIt0jhmKOilew9OS_jw3k3E5YRju3S5JryHZIngGdhkyGgv_HBhftvRxUOSLLI1Mk0ato3Lb3HfhxWLH0QUkPxW/s320/GRID102713_PIC1_missing+numbers+data+type+problem.JPG)
1) Numbers not showing up in savemodel - See fig 1
Date: Wednesday, Week 5, 10/30/13
I need go back and make sure the data types are preserved.
I used this answer:
http://www.mathworks.com/matlabcentral/answers/19489
To solve how to select row from uitable in MATLAB.
Errata List
1) When adding data from select_uitable to type_uitable, numbers become cells.
Undefined function 'plus' for input arguments of type 'cell'
When I put in blah+9, I get that message.
blah=get(handles.transrange_edit,'String');
disp(blah);
num=str2num(get(handles.transrange_edit,'String'));
I'm a visual debugger, as seen by the disp function.
Irvin's changes:
num=str2num(get(handles.transrange_edit,'String'));
becomes:
num=get(handles.transrange_edit,'String');
Date: Thursday, Week 5, 10/30/13
When I took out str2num, it broke previous functionality.
Now that I successfully set the strings to the values selected, it overwrote the strings already in there, which means my way is not the right way. I need to add my selected value with overwriting the current values.
Date: Monday, Week 4, 10/20/13
Goal: Get selector's save to text function and pull data from one part of GUI to another function done by Wednesday, October 23.
Deliverable: GUI that will produce the following in text file:
[Selector Data]
646,A, 7, 25, 1, 15, 2, 20
Steps to implement
1) Pull data from one part of GUI to another - type_uitable to select_uitable
1,15
2,20
needs to be come
1,15,2,20
or
A
B
needs to be
A,B
2) Take select_uitable data and save to text file.
3) Add load function to opening fcn of selector so that it can read saved files
4) ???
5) Profit
Errata List
1) 4x2 ghost data still in table despite deleting from fig file
types=get(handles.type_uitable,'Data');
data=cat(1,data,{node{:},phase{:},transnum,transrate,types});
K>> data
data =
'' '' '' '' ''
'' '' '' '' ''
'' '' '' '' ''
'' '' '' '' ''
data SHOULD be 1,15,2,20 (based on deliverable remember?)
Yeah, I'll finish it one day. One day.
Date: Friday, Week 4, 10/25/13
Goal unmet from last time: Get selector's save to text function and pull data from one part of GUI to another function done by Saturday, October 26.
Deliverable: Same as before.
Note: It is hard to work as efficiently as before since I don't remember the inputs and outputs. If I make a paper chart of the inputs and outputs, I have to remember to take it with me wherever I go.
1) Error using fgets
Invalid file identifier. Use fopen to generate a valid file identifier.
1] I traced the errors line by line until I came to the shocking conclusion that the GUI was referencing a file that wasn't there. D'oh! Lame lame lame error. -_-
2) 187 data=cat(1,data,{node{:},phase{:},transnum,transrate,{types}});
Error using cat
Dimensions of matrices being concatenated are not consistent.
Now I need to figure out how to change 5x2 cell into a single string to display.
Or: pull data from a structure into a string to display.
I'll come back to this...I swear. :/
Date: Saturday, Week 4, 10/26/13
Goal unmet from last time: Get selector's save to text function and pull data from one part of GUI to another function done by today.
Deliverable: Same as before.
Y x 2 cell array needs to be converted into a single string.
Ding, code is done, code is done!
types_string='';
[p q]=size(types);
for y=1:p
for x=1:q
types_string=[types_string,types{y,x},','];
end
end
What is pre allocation? MATLAB says I need to assign memory to my variable and then set it to 0 after so that it doesn't have to keep adding memory? I feel so n00b in programming...
Next step: add save button and save to GUI data: data_selector
1) Adding save button from tooltip does not work. Have to create a button and call it save.
function psave_Callback(hObject, eventdata, handles)
% hObject handle to psave (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selector_data=get(handles.select_uitable,'Data')
hmodel=getappdata(0,'hModel')
setappdata(hmodel,'selector_data',selector_data);
Verify if selector data is what we want:
343 selector_data=get(handles.select_uitable,'Data')
selector_data =
'633' 'A' [7] [35000] [1x44 char]
'633' 'A' [7] [35000] [1x44 char]
'633' 'A' [3] [15000] [1x44 char]
Hell yes!
2) add lines in model.m to get data from GUI data and print to .m text file
get data from GUI data = break down cell into lines? and store them line by line? How did I do this for typehouse?
In type house, Fred used 3 for loops to read a Y x 3 cell array.
This means I will need 5 for loops to read a Y x 5 cell array.
I wonder if there is a better way to do this than 5 for loops.
How do you access a cell within a cell?
cell{X,Y}{X,Y}
The save to text function rewrites all the data over again! It doesn't scan for existing data and then adds in the new stuff, it adds everything!
Just mimic typehouse because they are both cell arrays.
See Errata List #1
Step after that: Add read function in model.m to get data from text file to GUI data: data_selector
Dance because I am awesome and have finished basic debugging/"programming" for MATLAB and am now ready to go into Facebook as an 1337 h4ck3r.
/Sarcasm
Errata List
1) Not able to access entire row in data_selector due to varying data types
example: I want to print the first row of data_selector, a cell array.
It SHOULD be: A, B, C
but it REALLY is: C
1] I made data_selector into a cell
data_selector =
'633' 'A' [7] [35000] [1x44 char]
'633' 'A' [7] [35000] [1x44 char]
'633' 'A' [3] [15000] [1x44 char]
data_selector{1,:}= '633' 'A' [7] [35000] [1x44 char] GOOD
data_selector{1,:}= [1x44 char] BAD
2) Incorrect use of fprintf
fprintf(stuff) = WRONG
fprintf(fid, %d = int, %s = string, %f= floating) RIGHT
3) Incorrect storage of data
Loading the data from the text file:
[Selector Data]
632,A, , ,new,,Type 1,59, (line A)
632,A, , ,new,,Type 1,59,
Line A is stored as 1x1 cell, which IS NOT RIGHT.
It SHOULD be stored as a 1x5 cell.
ALSO: new should be taken out.
I'm stuck on what to do with this cell situation.
Is there any way I can write to the text file as a cell array so that reading it will result in...a cell array?
I do NOT want to do the typehouse approach...
I guess I could since I know where every column is going...
As in...IF i see 6XX, then assign every line after that to a new column.
The condition would be "pos=strfind(tline,'6')"
Hmmm...that could work. Should I keep working past my midnight curfew? Yeah let's do it! I slept more than enough today anyways.
Date: Sunday, Week 4, 10/27/13
Finish your shit from before.
INPUT: (X x 5 array)
'633' 'A' [3] [15000] [1x44 char]
OUTPUT:
633
A
3
15000
Type 1, X, Type 2, Y
SOLUTION:
for x=1: size(array,1)
for y=1: size(array,2)
fprintf(array{x,y})
end
end
Then:
INPUT:
633
A
3
15000
Type 1, X, Type 2, Y
OUTPUT:
X x 5 array
'633' 'A' [3] [15000] [1x44 char]
if ([House Type])
array={5,y}
Should I set the array size?
while (~isempty(pos=strfind(tline,'6')))
array{1,1}=tline
tline=fgetl(fid)
array{1,2}=tline
etc until
array{1,5}=tline
THEN LOOP BACK TO 6 condition
setappdata(hmodel,'selector_data',selector_data)
selector_data needs to be X x 5 cell
Errata List
Fig 1
1) Numbers not showing up in savemodel - See fig 1
Date: Wednesday, Week 5, 10/30/13
I need go back and make sure the data types are preserved.
I used this answer:
http://www.mathworks.com/matlabcentral/answers/19489
To solve how to select row from uitable in MATLAB.
Errata List
1) When adding data from select_uitable to type_uitable, numbers become cells.
Undefined function 'plus' for input arguments of type 'cell'
When I put in blah+9, I get that message.
blah=get(handles.transrange_edit,'String');
disp(blah);
num=str2num(get(handles.transrange_edit,'String'));
I'm a visual debugger, as seen by the disp function.
Irvin's changes:
num=str2num(get(handles.transrange_edit,'String'));
becomes:
num=get(handles.transrange_edit,'String');
Date: Thursday, Week 5, 10/30/13
When I took out str2num, it broke previous functionality.
Now that I successfully set the strings to the values selected, it overwrote the strings already in there, which means my way is not the right way. I need to add my selected value with overwriting the current values.
The reason why the boxes disappear is when the input does not match the current value displayed.
I figured this out by observing that adding A to a box with B selected removes the box.
In order to fix this, I need to match the values given to the values in the string and then change the active value VS
Date: Monday, Week 6, 10/30/13
1) Parenthesises vs curly brackets
Cell Array (1) = first cell in a cell array
Cell Array{1} = content within that first cell in the cell array
OOHHHH. DUR!
2) Char()
char() turns a cell array into a character array. I used it to turn this list of cells into a list of chars so I can use strmatch:
phase_list=char(get(handles.phase_popupmenu,'String'));
phase_value=strmatch(Data{Indices(1),2},phase_list)
3) Passing data between callbacks via handles
handles=guidata(hObject) = call it
handles.BLANK = data you want to store
guidata(hObject) = update
Where I leave off:
This thing never dies huh?
Date: Tuesday, Week 7, 11/12/13
1) Identify problem from before and fix it. Duh.
Done!
The problem was I forgot to capitalize my i
y_value=handles.Indices(1);
Attempted to access Indices(1); index out of bounds because
numel(Indices)=0.
2) Move data from one place to another
Sorting problem with ',' as delimiter.
1 solution which works only because there are two types of data: if else sorter.
if strmatch(Type)
put in this array
else
put in other array
Oh wait that doesn't work because it has to loop (for loop) but it's also parsing one single line of characters.
Which means I have to sort one line of characters separated by comma delimiters (sometimes not) into an cell array that's two wide and of unknown length.
Perhaps it would be better to add a space for empty values instead of leaving it with no space. This negates the point of the delimiter.
A, B, A, B
to
A B
A B
Strsplit FTW!
Now how to morph results of strsplit:
'Type 1' ' ' 'Type 2' ' '
to
'Type 1' ' '
'Type 2' ' '
Screw this crap, I'm going to write a function!
I'm too distracted by personal matters to be able to focus now. Will finish this section tomorrow.
reminder: I put in my lines of code with "% Irvin 111213"
No comments:
Post a Comment