Accessing Character Vectors Within Cell Arrays, Output Is Size of Character Vector, not Contents?

2 visualizaciones (últimos 30 días)
I am on an older version of MATLAB (R2014A), and trying to output text that is stored in character vectors that are stored in a cell array.
I noticed that when I enter the character arrays manually there is no issue:
myStrings = repmat({''}, 1, 10);
for i = 1:10
myStrings{i} = 'This character array is quite lengthy but still prints to screen normally.';
end
myStrings'
Output: Column vector with the above sentence in every cell. (I want this)
But when I do something similar MATLAB outputs the size of each character array instead of the contents. [SomeStructure.SomeData is a cell vector where each cell is a character vector]
myStrings2 = repmat({''},1,10);
for i = 1:10
myStrings2{i} = SomeStructure.SomeData{i};
end
myStrings2'
Output: A column vector with entries simply listing the size of the char array (i.e [1x25 char]). (I get this)
When I view this SomeStructure.SomeData in my workspace, I see the strings I want, but when I call it in the console it also outputs these sizes. How do I get these strings/character vectors to behave the way I want? i.e, like the first block of code?
Other maybe helpful notes:
  1. When I call SomeStructure.SomeData, to the console one of the entries is a regular string and not the size of the string (last entry).
  2. I get the data structure by reading from input files using:
function [PS_Data] = getPSData(N,directory)
%N is number of rows in data input files for PS
%direct is directory where .txt files are stored
oldfolder = pwd; %keep track of old directory
cd(directory); %change dir to file directory
files = dir('*.txt'); %grabs all files in current directory
for k = 1:length(files)
file = files(k).name; %look at a specific file
fid = fopen([pwd '\' file]); %open it
my_field = strtok(files(k).name,'.'); %chops off '.txt' for variable name
for i = 1:N
PS_Data.(my_field){i} = fgets(fid);
end
fclose(fid); %close file
end

Respuesta aceptada

Joel Bay
Joel Bay el 27 de Jun. de 2019
Editada: Joel Bay el 27 de Jun. de 2019
The work around I ended up going with to sovle this issue was directing the user to the work space, but I figured out the reason why, hopefully this helps someone in the future.
fgets includes the newline character at the end of your string if there is one in your input file (at least in 2014a), which makes the strings too long and will not display to console when trying to view the entire cell array. So in a sense the other poster was correct, however, this is easily remedied using strtrim().
  1 comentario
dpb
dpb el 27 de Jun. de 2019
Just use fgetl() instead. Rarely does one really want the control characters inside the file; echo-ing the file in a copy operation or the like is about the only time would ever be any advantage in doing so. Otherwise, just read the data.

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 17 de Jun. de 2019
While not your specific question, couldn't help but recast your function using standard MATLAB functions for various operations with file names.
In general, not a wise idea to cd; use a fully-qualified filename instead. fullfile is very useful for purpose as is fileparts for the converse operation. NB: your function, while recording the working directory on entry, never restored the environment before leaving...rude! :)
function [PS_Data] = getPSData(N,directory)
%N is number of rows in data input files for PS
files = dir(fullfile(directory,'*.txt')); % all files in directory
for k = 1:length(files)
fid=fopen(fullfile(pwd,files(k).name));
[~,my_field]=fileparts(files(k).name);
for i = 1:N
PS_Data.(my_field){i} = fgets(fid);
end
fclose(fid); %close file
end
As to the question itself, it's just a fignewton of the display of various data types in ML command window. The data in the struct will be complete but many of the displays when data are long are summarized to show summary of content rather than the full contents when are large. There's nothing you can do at the command line to change this default display behavior other than explicitly dereference the cell content to the character vector underlying it.
  1 comentario
Joel Bay
Joel Bay el 17 de Jun. de 2019
Oops, I forgot to include the cd(oldfolder) at the end of my function, regardless thanks for the tip on best practice!
So I did suspect that the data may just be too long like you are saying, but actually that sentence I enter manually in the example is able to be displayed without issue, and it is longer than the character arrays I am trying to display. Is there any way it could be an error on my part and not a delicious cookie?

Iniciar sesión para comentar.

Categorías

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

Productos


Versión

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by