Azzera filtri
Azzera filtri

How to create an indexable array of fid

1 visualizzazione (ultimi 30 giorni)
Raymond Chiu
Raymond Chiu il 31 Ago 2023
Commentato: Stephen23 il 31 Ago 2023
To open a file:
fid = fopen('speedygo_nCores.bat','wt');
and given:
nCores=6;
What is the syntax to declare a fid array of size nCores? So as to be able to implement:
for i=1:nCores
goFilename = strcat('speedygo_' , i, '.bat' );
fid(i)=fopen(goFilename,'wt');
end
The goal is to enumerate the fid(i), where i = {1,2,3...nCores} and for use in fprintf:
fprintf(fid(i),"hello world");
Can a cell hold the six fids? or is there a better technique?

Risposte (1)

Matt J
Matt J il 31 Ago 2023
fids are just numbers. You can allocate any double array to hold them.
  3 Commenti
Walter Roberson
Walter Roberson il 31 Ago 2023
nCores = 6;
fid = zeros(nCores,1);
for i=1:nCores
goFilename = "speedygo_" + i + ".bat";
fid(i) = fopen(goFilename,'wt');
end
pre-allocation is just a matter of efficiency here.
However when you pass in i to strcat like that, the result would be the same as if you had done
goFilename = ['speedygo_' , char(i), '.bat' ];
as in the first one being speedygo_ followed by Start of Heading character https://www.compart.com/en/unicode/U+0001
Using an integer in strcat() or [] with character vectors converts the integer to its exact character code, and does not convert the integer to the ascii representation of the digits. char(1) is not the same as '1' which is char(49)
Stephen23
Stephen23 il 31 Ago 2023
"pre-allocation is just a matter of efficiency here."
Preallocation also significantly increases robustness.

Accedi per commentare.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Tag

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by