How to insert space to a string given

12 ビュー (過去 30 日間)
Buddhini Angelika
Buddhini Angelika 2016 年 2 月 20 日
回答済み: Jan 2016 年 2 月 21 日
Recently I have made a gui which allows users to input sentences with spaces in capital letters and stores in a varitable text1. As an example
text1=MY FIRST GUI.
Then it removes the space present in the sentence by the following command and uses it for some implementation and transfers it to another gui.
text1(findstr(text1,''))=[];
Then in the new gui there is a text box where this text1 is to be displayed but I need to display it including the spaces that were present before in the original sentence. Is there a way to insert those spaces exactly at the original locations by using some commands? ( I want to transfer the existing text1 to a text with spaces without getting the original one itself from the first gui)
I am working in MATLAB R2013a. Please help me with this problem.
Thanks a lot in advance.
  1 件のコメント
Jan
Jan 2016 年 2 月 20 日
findstr is deprecated for many years now, use strfind instead. The shown command does not remove anything, because the empty string is searched. Is this a typo and you meant:
text1(findstr(text1,' ')) = [];

サインインしてコメントする。

回答 (4 件)

Stephen23
Stephen23 2016 年 2 月 21 日
As long as you know the locations of the space characters, then it is easy to recreate the original string:
>> str = 'MY FIRST GUI';
>> idx = str==' '; % identify spaces
>> tmp = str(~idx) % remove spaces
tmp =
MYFIRSTGUI
Now using only tmp (the string with no spaces) and the indices |idx) we can recreate the original string:
>> new(~idx) = tmp; % string without spaces!
>> new(idx) = 32 % add spaces
new =
MY FIRST GUI

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 2 月 20 日
If you haven't the original text, how Matlab will know where the spaces are located? At least when you remove the spaces, save the indices of spaces that were removed
  3 件のコメント
Image Analyst
Image Analyst 2016 年 2 月 21 日
Like I said, you should still have the original string so why do you need to insert them in the stripped string?
Buddhini Angelika
Buddhini Angelika 2016 年 2 月 21 日
In this programme I am trying to display some kind of a process. It has some details so its bit difficult to type them all here.
What I can do is in the first gui I can store the locations of spaces in a vector and transfer it to the second gui where only the text without spaces is present. Then is there a way to insert the spaces back.
Thank you very much.

サインインしてコメントする。


Image Analyst
Image Analyst 2016 年 2 月 21 日
Try this
% Get the text the user typed from the edit box
textString = get(handles.edit1, 'String');
% Now remove all spaces:
textString(textString == ' ') == [];
% Display the string with the spaced back in there.
% This is done simply by sending the original string (not the altered string)
% to the edit field or static text field.
set(handles.text1, 'String', textString);

Jan
Jan 2016 年 2 月 21 日
Storing the indices of the spaces seems to be an indirection. What about storing the original strings in addition? Providing the strings with the spaces sounds easier also. Then the 2nd GUI removes the spaces by its own dynamically.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by