Create cell array with the same string n-times

Hi all,
I have another problem I haven't found any soulution for yet.
I have a list of Values and I have the name of the .mat-file the values are saved in as a string.
For example
name='Values.mat'
values =
1
2
3
4
5
Now I need to create a cell array with the name of the file for each value.
For example
>> values.filename
ans =
Values.mat Values.mat Values.mat Values.mat Values.mat
Does anybody know how I can create that cell array, size depending on how many values I have?
Thanks a lot! Oli

 Respuesta aceptada

Daniel Shub
Daniel Shub el 2 de Oct. de 2011
The names of your variables are a little confusing ... You are starting with values being a double array and then wanting values to be a structure ...
To answer your question you can create a cell array with the same string n-times with deal.
doc deal
Specifically you can make a 1x5 cell array filename have the value saved in name with:
[filename{1:5}] = deal(name);
you may want to consider a structure array ...
values = struct('value', mat2cell(values, 1, ones(5, 1)), 'filename', name)

2 comentarios

Olli
Olli el 2 de Oct. de 2011
Movida: Dyuman Joshi el 28 de Feb. de 2024
Thanks a lot! Sorry for the confusing description, but your responses were very helpful! Oli
morteza HEIDARI
morteza HEIDARI el 13 de Feb. de 2017
thank you man. it was helpful.

Iniciar sesión para comentar.

Más respuestas (3)

Jan
Jan el 2 de Oct. de 2011
C = cell(1, 5);
C(:) = {'String'};

11 comentarios

Jonathan Deaton
Jonathan Deaton el 8 de Dic. de 2015
You are the real winner here.
Youcef Yahiaoui
Youcef Yahiaoui el 15 de Mzo. de 2016
Jan Simon's answers are always the best. Whenever I look anything up, I always look for this distinct picture! Thanks, Jan Simon.
Yifei Wang comments to Jan:
short and good as always
Jan
Jan el 4 de En. de 2017
Thanks. I forward the compliments to Matlab, which allows such compact and nive solutions. Try the same in C... ;-)
Pearl
Pearl el 30 de En. de 2019
awesome! Another thanks to you!
Jon
Jon el 10 de Jul. de 2019
very nice technique - found this before, forgot it, and just came back again - thank you
BN
BN el 11 de Feb. de 2020
Dear Jan,
Thank you, it solve my problem too. I just want to say thanks to you.
Best Regards
Saeid
Saeid el 24 de Sept. de 2020
Jan's always there to give the shortest version of every solution. This helped me too!
Fabio Rondina
Fabio Rondina el 24 de Sept. de 2021
still usefull after 10 years. Thanks @Jan
Rob
Rob el 17 de Dic. de 2021
Nice simple solution but it baffles me why Matlab needs two lines of code to do a simple task (or why the default is to set the contents as doubles)?
M_A_C
M_A_C el 28 de Feb. de 2024
Hello Jan,
Your elegant solution worked for me using the function strings.

Iniciar sesión para comentar.

dscharf
dscharf el 17 de Ag. de 2023
Editada: Dyuman Joshi el 28 de Feb. de 2024
Riff'ing on @Jan's answer and noting @Rob's comment,
c(1:5) = {'string'}
c = 1x5 cell array
{'string'} {'string'} {'string'} {'string'} {'string'}
gives a 1x5 in one line (assuming c hasn't been defined before) and
d(1:5,1) = {'string'}
d = 5x1 cell array
{'string'} {'string'} {'string'} {'string'} {'string'}
gives a 5x1. Or at least they do in 2022a.

Categorías

Etiquetas

Preguntada:

el 2 de Oct. de 2011

Movida:

el 28 de Feb. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by