Does Matlab support strcpy?

17 ビュー (過去 30 日間)
Mini Me
Mini Me 2013 年 2 月 5 日
I was just wondering if Matlab support strcpy. In writing a program and I want to cot a string and I was wondering if I could use strcpy which is a C command. So far it does not give me an error but it's not doing anything either
  2 件のコメント
Cedric
Cedric 2013 年 2 月 5 日
編集済み: Cedric 2013 年 2 月 5 日
Could you provide an example of what you want to do? If it is just concatenation with e.g. "", you can do something like
quotedStr = ['"', initialStr, '"'] ;
or
quotedStr = sprintf('"%s"', initialStr) ;
Ah, if 'cot' was 'cut' and not 'quote', you can index a string like a numeric array..
>> myString = 'Hello World' ;
>> myString(1:5)
ans =
Hello
Mini Me
Mini Me 2013 年 2 月 5 日
I want to copy a string from a file. While(fgets(line ,size of line, file)!=NULL) strcpy(array, line). But in Matlab I change it to While (fgets(line, size(line),file )~=0); Strcpy(array,line). Then print that string. That's just part of the code when I run It it doesn't give me an error not does it print it

サインインしてコメントする。

回答 (4 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 5 日
編集済み: Azzi Abdelmalek 2013 年 2 月 5 日
strcpy is not a matlab function, and it should give an error, how are you using it?

Sean de Wolski
Sean de Wolski 2013 年 2 月 5 日
Looking at: strcpy (and not being a c++ expert) it looks like you don't even need it. MATLAB passes by reference automatically, so just:
str2 = str;
would do the same thing.

Mini Me
Mini Me 2013 年 2 月 5 日
I want to copy a string from a file. While(fgets(line ,size of line, file)!=NULL) strcpy(array, line). But in Matlab I change it to While (fgets(line, size(line),file )~=0); Strcpy(array,line). Then print that string. That's just part of the code when I run It it doesn't give me an error not does it print it
  1 件のコメント
Jan
Jan 2013 年 2 月 5 日
This is not a valid Matlab program. Checking the reply of FGETS to differ from 0 is a bad idea, because strings are allowed to contain zeros in Matlab and WHILE isnerts an all() implicitly when its argument is a vector. And then even Strcpy() is not a valid Matlab command.

サインインしてコメントする。


Jan
Jan 2013 年 2 月 5 日
編集済み: Jan 2013 年 2 月 6 日
while 1 % Infinite loop
line = fgets(fid);
if ~ischar(str)
break;
end
array = line;
... process the array here
end
I assume your concept of using STRCPY is not useful in Matlab.
Btw, I used "line" to be near to your example. But it ios recommended not to use the names of built-in functions in real programs.
[EDITED] Perhaps you want:
fid = fopen(FileName, 'r');
A = fscanf(fid, '%f', Inf);
fclose(fid);
  2 件のコメント
Mini Me
Mini Me 2013 年 2 月 5 日
Thanks jan, it somewhat close to what I'm trying to do. Except I'm trying to grab those strings arrays on different line and assign them to a variable name. For example I have a loop that increments and goes to each line. So if And also a loop for the array size For k=0:1:10 If array{k}=array{0} Then copy it. Print it. Sscanf(array{0},%f,var) to grab the strings and pass it to variable that's Already declared in function. That's just an oversize of what I'm Tryna do
Jan
Jan 2013 年 2 月 6 日
"array{k} = array{0}" is simply confusing. Do you want "array" to be a cell? If not, please do not use curly braces in a Matlab forum. In addition Matlab's indices are 1-based, so there is no element with index 0. Neither "then copy it" nor "print it" are clear. And "Sscanf(array{0},%f,var)" is still mysterious for me. So please explain, what you are trying to do without using another language.
Terms like "Tryna" might be kewl, but they are hard to understand for non-native speakers.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by