Store and Retrieve simple text box content using GUI

Dear users
I have a question about store and retrieve IDs and names by using matlab , following my simple GUI and there are only two (text box 1 , text box 2) when i entered some authors names along with its ids ,
then for search purpose I need to enter only the ID then when i press search will retrieve the name correspond to that id , is there any way to do that i will thank any one can help me in this manner ..

 채택된 답변

Adam
Adam 2016년 3월 15일
편집: Adam 2016년 3월 15일
doc containers.map
should help with this if your IDs are not guaranteed to be contiguous and starting from 1. There are examples in the help and using numeric indices is very easy anyway. It has the advantage over an array that your indices can be any numeric (or string) value.
If you are struggling with the underlying GUI aspect of it then that is a different matter, but a containers.Map object should work well to store all your data, assuming IDs are unique which seems a safe assumption given you are searching based on them.

댓글 수: 4

Thanks sir , for writing values of text box 1 and 2 to mat. file its easy for me, but for search command i need some help for that , ID its only numbers start from 1... 1000 , and continuously increased by user .
Adam
Adam 2016년 3월 15일
편집: Adam 2016년 3월 15일
Using containers.Map will make search trivial. You can just index directly to the record you want. There is no searching involved.
If your IDs will be contiguous numeric from 1 upwards and you will not be deleting any of them though then just use a cell array. Again no searching is required because your ID will simply be the index into the cell array and its contents will be the result you want to retrieve.
Whether you choose to save the map or array to .mat file is entirely up to you and is independent of how you do the indexing into it other than that you have to read it in every time you use it and write it every time you add a new record which may be costly.
If your records are simply strings why not just store it in memory on the handles structure of your GUI? You can then save to mat file on closing the GUI if you wish, although I don't know if currently your GUI has any way to choose an existing set of records to load from file on opening.
Sir , first option its appropriate for me , i reading container documentation now , i need just William staling indexing with 1 , John Makin indexing with 2 ... so on , for search i changed to simple retrieve when i enter number 1 in (text box 3) then William Staling is shown in text box 2 ? i will thank you if you have simple code can do that , i'm newbie to matlab and i need this work for my school duties
In your openingFcn you should declare the map - e.g.
handles.mappings = containers.Map;
Then in your 'Add' callback you simply do something like:
id = str2double( get( handles.editId, 'String' ) );
name = get( handles.textName, 'String' );
handles.mappings( id ) = name;
guidata( hObject, handles );
In your search callback something like:
id = str2double( get( handles.editId, 'String' ) );
name = handles.mappings( id );
set( handles.textName, 'String', name );
That is a rough and ready untested example of the type of code that is needed. To be robust you need to do a check using isKey( ) on the map before trying to retrieve the name from the mappings because if the key does not exist within the map you will get an error. If you test for the key then you can use
doc errordlg
for example to present a relevant message to the user.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

제품

태그

질문:

2016년 3월 15일

댓글:

2016년 3월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by