Suppressing braces and single quotes of a string matrix

조회 수: 57 (최근 30일)
David Cole
David Cole 2024년 10월 25일 20:54
댓글: Umar 2024년 10월 27일 9:59
How do I suppress the braces and single quotes in a matrix full of strings?
  댓글 수: 1
Stephen23
Stephen23 2024년 10월 25일 21:55
"How do I suppress the braces and single quotes in a matrix full of strings?"
The term "suppress" is unclear. What is your actual goal?:
  1. do you wish to change how the data are displayed in the command window?
  2. are you attempting to remove those characters from the text data itself? (note: most likely they are not part of the text data)
As others have noted, your data appear to be a cell array of character vectors, not a string array:

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

답변 (4개)

Steven Lord
Steven Lord 2024년 10월 25일 20:59
That's not a string matrix. That's a cell array each cell of which contains a char vector. Commonly that's referred to as a cellstr.
To convert a cellstr into a string array, call string on it.
CS = {'abracadabra'; 'hocus pocus'}
CS = 2x1 cell array
{'abracadabra'} {'hocus pocus'}
S = string(CS)
S = 2x1 string array
"abracadabra" "hocus pocus"
If you want to display the string array without the double quotes you can fprintf it. This doesn't change the data stored in S, it just displays it differently.
fprintf("%s\n", S)
abracadabra hocus pocus
S % data is unchanged
S = 2x1 string array
"abracadabra" "hocus pocus"

Voss
Voss 2024년 10월 25일 21:01
C = { ...
['V1 = 1.01' char(8736) '0' char(176) ' pu']; ...
['V2 = 0.92675' char(8736) '-5.7691' char(176) ' pu']; ...
} % etc.
C = 2x1 cell array
{'V1 = 1.01∠0° pu' } {'V2 = 0.92675∠-5.7691° pu'}
fprintf(1,'%s\n',C{:})
V1 = 1.01∠0° pu V2 = 0.92675∠-5.7691° pu

Walter Roberson
Walter Roberson 2024년 10월 25일 21:12
CS = {'abracadabra'; 'hocus pocus'};
disp(char(CS))
abracadabra hocus pocus

Umar
Umar 2024년 10월 26일 2:24

Hi @David Cole ,

You asked, “How do I suppress the braces and single quotes in a matrix full of strings?”

To achieve the desired output in MATLAB, you need to manipulate the cell array of strings that contains the bus voltages. The primary goal is to convert the cell array into a standard format that displays the strings without braces { } and single quotes ''. Below is a detailed explanation of how to accomplish this, along with the complete code. First, define a cell array that contains the bus voltage strings. This is the initial format we will work with. Then convert the cell array into a character array or a string array, which will allow you to manipulate the strings more easily. Finally, display the strings in a clean format, ensuring that no braces or quotes are present. Here is the complete MATLAB code that implements the above steps:

% Step 1: Define the cell array of bus voltages
busVoltages = {
  'V1 = 1.0120° pu';
  'V2 = 0.926752-5.7691° pu';
  'V3 = 1.0527-4.4235° pu';
  'V4 = 0.907172-3.7558° pu';
  'V5 = 1.0522-2.3978° pu';
  'V6 = 0.903952-4.0345° pu'
};
% Step 2: Convert the cell array to a string array
busVoltagesStr = string(busVoltages);
% Step 3: Display the strings without braces and quotes
for i = 1:length(busVoltagesStr)
  fprintf('%s\n', busVoltagesStr(i));
end

Please see attached.

In the above code snippet, busVoltages variable is defined as a cell array containing the bus voltage strings. Each string is enclosed in single quotes and separated by semicolons. The string ( ) function is used to convert the cell array into a string array. This conversion simplifies the process of displaying the strings. A for loop iterates through each element of the string array. The fprintf function is used to print each string to the command window without any additional formatting characters. The format specifier %s ensures that only the string content is printed.

By following the steps outlined above, you can effectively suppress the braces and single quotes from a matrix of strings in MATLAB.

Hope this helps.

Please let me know if you have any further questions.

  댓글 수: 4
Stephen23
Stephen23 2024년 10월 27일 8:10
편집: Stephen23 2024년 10월 27일 8:20
Converting to string is also superfluous (as Voss already demonstrated):
busVoltages = {
'V1 = 1.0120° pu';
'V2 = 0.926752-5.7691° pu';
'V3 = 1.0527-4.4235° pu';
'V4 = 0.907172-3.7558° pu';
'V5 = 1.0522-2.3978° pu';
'V6 = 0.903952-4.0345° pu'
};
fprintf('%s\n',busVoltages{:});
V1 = 1.0120° pu V2 = 0.926752-5.7691° pu V3 = 1.0527-4.4235° pu V4 = 0.907172-3.7558° pu V5 = 1.0522-2.3978° pu V6 = 0.903952-4.0345° pu
Umar
Umar 2024년 10월 27일 9:59
Hi @Stephen23,
I understand your point, converting the cell array to a string is indeed unnecessary, as fprintf can handle the cell array directly, maintaining clarity and efficiency in your code. It is mentioned in @Walter Roberson’s comments as well. Excellent team work helping out each other. Really appreciate it.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by