Suppressing braces and single quotes of a string matrix

3 visualizaciones (últimos 30 días)
David Cole
David Cole el 25 de Oct. de 2024
Comentada: Umar el 27 de Oct. de 2024
How do I suppress the braces and single quotes in a matrix full of strings?
  1 comentario
Stephen23
Stephen23 el 25 de Oct. de 2024
"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:

Iniciar sesión para comentar.

Respuestas (4)

Steven Lord
Steven Lord el 25 de Oct. de 2024
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 el 25 de Oct. de 2024
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 el 25 de Oct. de 2024
CS = {'abracadabra'; 'hocus pocus'};
disp(char(CS))
abracadabra hocus pocus

Umar
Umar el 26 de Oct. de 2024

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 comentarios
Stephen23
Stephen23 el 27 de Oct. de 2024
Editada: Stephen23 el 27 de Oct. de 2024
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 el 27 de Oct. de 2024
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by