using strcat with num2str

5 visualizzazioni (ultimi 30 giorni)
kareem salloomi
kareem salloomi il 29 Ago 2016
Risposto: Walter Roberson il 30 Ago 2016
I'm new to Matlab and I wanna know what does the below command or function means.
filename = strcat(filename(1:(105-15)),num2str(i),'130')

Risposte (2)

Walter Roberson
Walter Roberson il 30 Ago 2016
You have
filename='C:\X\fort.130';
filename = strcat(filename(1:(105-15)),num2str(i),'130');
The first of those lines makes filename a string of 13 characters. The second of those lines tries to access the first 90 of those 13 characters.
Please have a look at

KSSV
KSSV il 29 Ago 2016
strcat - Concatenates strings in your code the strings are ...first 90 characters from the string filename, character i and number character 130.
num2str : converts number to string/ character.
  2 Commenti
kareem salloomi
kareem salloomi il 30 Ago 2016
Modificato: Walter Roberson il 30 Ago 2016
Thanks Dr. Siva for your kind reply. Actually I have a data file with the extension of .130 its a fortran data file and I'm using the below matlab code to import these data. unfortunately, the command
filename = strcat(filename(1:(105-15)),num2str(i),'130')
is working properly and return the following error: "Index exceeds matrix dimensions".
clear all; close all; clc;
%%Initialize variables.
D=dir('C:\X\fort.*');
num=length(D(not([D.isdir])));
filename='C:\X\fort.130';
theta1=18;
theta2=42;
theta3=78;
theta4=102;
Phi_offset_F=120;
Phi_offset_S=240;
for i=1:1:num
filename='C:\X\fort.130';
filename = strcat(filename(1:(105-15)),num2str(i),'130');
fprintf('%2f\n',i/num*100);
delimiter = ' ';
%%Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%s%s%s%s%s%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r');
%%Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'ReturnOnError', false);
%%Close the text file.
fclose(fileID);
%%Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
% Converts strings in the input cell array to numbers. Replaced non-numeric
% strings with NaN.
rawData = dataArray{2};
for row=1:size(rawData, 1);
% Create a regular expression to detect and remove non-numeric prefixes and
% suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData{row}, regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if any(numbers==',');
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(thousandsRegExp, ',', 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric strings to numbers.
if ~invalidThousandsSeparator;
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, 2) = numbers{1};
raw{row, 2} = numbers{1};
end
catch me
end
end
%%Split data into numeric and cell columns.
rawNumericColumns = raw(:, 2);
rawCellColumns = raw(:, 1);
%%Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),rawNumericColumns);
R2 = cellfun(@(x) ~isnumeric(x) && ~islogical(x),rawCellColumns);% Find non-numeric cells
rawNumericColumns(R) = {NaN};
rawCellColumns(R2)={NaN}; % Replace non-numeric cells
x=length(rawCellColumns);
%%Allocate imported array to column variable names
The = cell2mat(rawCellColumns(:, 1));
Timeaveraged = cell2mat(rawNumericColumns(:, 1));
%Poyntine = rawCellColumns(:, 2);
%vectorWmm = rawCellColumns(:, 3);
%at = rawCellColumns(:, 4);
%point = rawCellColumns(:, 5);
James Tursa
James Tursa il 30 Ago 2016
What does this print:
numel(filename)
Maybe filename doesn't have 105 characters.

Accedi per commentare.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by