How can I create a string scalar from a string array?
25 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Tilman Nelissen
el 31 de Oct. de 2022
I have data of absorbances of different samples stored in .csv files. I measure an absorbance spectrum at specific concentrations of different types of chemicals. My Filenames are all labeled in the same manner, for example: S_A_4%_Probe_1 or TCI_2%_Probe2.
I created a function, which should read these files in a folder and write them into a matrix which is then converted to a table. The problem is that I have different types of chemicals, which have different names. To search for these Names (which are e.g. "S_A") I wanted to create a String Array that contains the names of the chemicals that I've measured and which is given as an argument to the function. Now the problem arises that the following line of code gives me a string array rather than a string scalar, which is needed for this function (readmatrix) to work:
filenameconc = ['C:\Users\XXXX\', LigninName(1,i), '_', num2str(MeasuredConz(i, k)), '%_TN_LB_'];
How can I overcome this problem or what other possibilities are there to read the files with different names into the matrix?
function[Matrix_Absorptions] = AbsorptionRead(Repeats, MeasuredConc, LigninName,nmStart, nmEnd, nmStep)
%This function automatically reads the data from the spectrometer into the matrix
%"Matrix_Absorptions". Subsequently the matrix is read into a
%table and the column designation with the lignin names is taken from
%the vector "LigninName" is added.
%The files must be stored in the folder "'C:\Users\XXXX\" and start with the lignin name,
%follow with the concentration
%follow an ascending numbering and finish with _TN_LB.
%Example: TCI_2%_TN_LB_3
%
%Repetition = vector with number of repetitions per type of lignin, so if on two
%different days the same concentration of the same lignin has been measured
%were measured, a 2 is entered for this type of lignin.
%
%MeasuredConc = Matrix listing the measured concentrations (in the column)
%per lignin type. Thus, if of lignin "TCI" 1, 2, and 4%
%concentrations were measured, the first column lists 1; 2; 4
%is listed.
%
%LigninName = vector with the sequence of lignin names
%
%nmStart= nm from which the measurement starts
%
%nmEnd = nm, to where the measurement goes
%
%nmStep = how big is the distance between the single measured
%wavelengths
%For this code, the same repetitions per concentration, with
%same wavelength range, the same wavelength step, and with uniform labeling of the samples.
%Further, there must be
%repetitions with all concentrations and again the same number of
%measurements per concentration.
%Calculate the number of wavelengths measured individually.
Numberofwavelengths = abs(nmStart-nmEnd)/nmStep;
MeasurementPerConc = 4;
MaxTotalMeasurements = MeasurementPerConc * numel(LigninName) * numel(MeasuredConc) * max(Repeats);
%with max(Repeats) the maximum possible number of entries is assumed, so some
%columns could be left empty.
%Generation of the matrix, where the absorbance values will be written in
Matrix_Absorptions = zeros(MaxTotalMeasurements, Numberofwavelengths);
%Create a string array, which will later carry the names of the columns
TableLegend = strings(1,MaxTotalMeasurements );
for k = 1:numel(LigninName) %count each lignin type
for i=1:size(MeasuredConc,k) %counts off the concentrations of the lignins per lignin name (i.e. per k) So if there are three entries in column k, "3" is returned and the for-loop is run three times
%First the concentration is determined. So the filename is defined with
%num2str() so that it contains the concentrations from the vector
%MeasuredConc
filenameconc = ['C:\Users\XXXX\', LigninName(1,i), '_', num2str(MeasuredConz(i, k)), '%_TN_LB_'];
for j=1:Repeats(k) * MessungProKonz %counts the number of repetitions at the same concentration (on different days, not per concentration).
%because the same concentrations were measured four times each (without repetition),
%the numbering at the end of the file is used, and
%the files are written into the cell
filename = [filenameconc, num2str(j) , '.Sample.Raw.csv'];
%in the matrix Matrix_Absorptions are written per loop (forLoop) the
%Absorptions are entered into a new column. So there are in each case
%"NumberofMeasurements" (as variable) belong to one concentration.
Matrix_Absorptions(j,:) = readmatrix(filename, 'Range', 'B2:B277');
%The table labeling is initiated here. For this
%first the lignin name, then the measured concentration and
%then the sample number (e.g. 3) is entered.
TableLegend(1, j) = [LigninName(k), num2str(MeasuredConc(i, k)),num2str(j) ];
end %end 3. for
end %end 2. for
end %end 1. for
%Makes a table out of the matrix
T = array2table(matrix_absorptions);
%labels the first column with the generated names
T.Properties.VariableNames(1:MaximumTotalMeasurements) = TableLegend;
end %end function
0 comentarios
Respuesta aceptada
Voss
el 31 de Oct. de 2022
Editada: Voss
el 31 de Oct. de 2022
Use curly brace indexing on LigninName to get a character array:
filenameconc = ['C:\Users\XXXX\', LigninName{1,i}, '_', num2str(MeasuredConz(i, k)), '%_TN_LB_'];
Or use char() to convert the string LigninName(1,i) into a character array:
filenameconc = ['C:\Users\XXXX\', char(LigninName(1,i)), '_', num2str(MeasuredConz(i, k)), '%_TN_LB_'];
Or use the string concatenation operator (+):
filenameconc = 'C:\Users\XXXX\' + LigninName(1,i) + '_' + num2str(MeasuredConz(i, k)) + '%_TN_LB_';
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!