Error using dir Function is not defined for 'string' inputs - string

I'm writing my first Matlab script, and I get an error trying to use dir(). This is the script:
strLocation = "C:\Users\myname\Documents\MATLAB";
listing = dir(strLocation)
The error is:
Error using dir
Function is not defined for 'string' inputs.
What am I doing wrong?

It should first be noted that a char vector and a string are different things in Matlab. The string data type was introduced recently (in R2016b, I think). Previous versions do not support the string type, only char vectors.
Since the string data type was introduced, many built-in functions that used to accept char vector input can now accept string input as well. But this is being gradually incorporated into functions, apparently. So, even if your Matlab version supports the string data type, you may find some functions that still can only take a char vector as input. This seems to the case for dir in your version. In R2018b dir supports both types of input, according to the documentation.
So, you need to define the input to dir as a char vector. For this you use ' instead of ":
strLocation = 'C:\Users\myname\Documents\MATLAB';
listing = dir(strLocation)
Or, if you must have a string, convert it to a char vector before passing it to dir:
strLocation = "C:\Users\myname\Documents\MATLAB";
listing = dir(char(strLocation))

Since MATLAB R2017a double quotation marks denotes strings and single quotation marks denotes character vectors.
The dir function requires a char vector so you should call it with single quotation marks,
strLocation = 'C:\Users\myname\Documents\MATLAB';
listing = dir(strLocation)

Related

Reading a comma-separated string (not text file) in Matlab

I want to read a string in Matlab (not an external text file) which has numerical values separated by commas, such as
a = {'1,2,3'}
and I'd like to store it in a vector as numbers. Is there any function which does that? I only find processes and functions used to do that with text files.
I think you're looking for sscanf
A = sscanf(str,formatSpec) reads data from str, converts it according
to the format specified by formatSpec, and returns the results in an
array. str is either a character array or a string scalar.
You can try the str2num function:
vec = str2num('1,2,3')
If you have to use the cell a, per your example, it would be: vec=str2num(a{1})
There are some security warnings in the documentation to consider so be cognizant of how your code is being employed.
Another, more flexible, option is textscan. It can handle strings as well as file handles.
Here's an example:
cellResult = textscan('1,2,3', '%f','delimiter',',');
vec = cellResult{1};
I will use the eval function to "evaluate" the vector. If that is the structure, I will also use the cell2mat to get the '1,2,3' text (this can be approached by other methods too.
% Generate the variable "a" that contains the "vector"
a = {'1,2,3'};
% Generate the vector using the eval function
myVector = eval(['[' cell2mat(a) ']']);
Let me know if this solution works for you

How to pass a string variable(Not a string literal) to $dumpfile system task in SytemVerilog?

I am running simulations with different parameters passed to the test bench as plus args. I want to dump separate VCD for each of these runs. I tried declaring a string variable and construct the file name using the parameters passed, and pass this on to the $dumpfile.
string file_name;
file_name = "tx_dsp.vcd"
$dumpfile(file_name);
But I am getting the following error in IES:
Passing string variable to this system task/function is currently not supported
As a workaround I defined the file name from command line and used it as argument to $dumpfile. This works, but not if the test parameters were randomized from inside the test bench.
Is this the behaviour of simulator or SystemVerilog? Any solution?
Thanks.
According to the SystemVerilog LRM, it should be possible. In 21.7.1.1, it says the following:
dumpfile_task ::=
$dumpfile ( filename ) ;
The filename is an expression that is a string literal, string data
type, or an integral data type containing a character string that
names the file to be opened. The filename is optional and defaults to
the string literal "dump.vcd" if not specified.
You are using a string data type in your example (Section 6.16 in the aforementioned document). An advantage is that the length of the string is dynamic and that no truncation can occur.
String literals (Section 5.9 in the LRM), on the other hand, behave like packed arrays. If your compiler does not support string data types in $dumpvars, you can try to define file_name as a string literal:
reg[N*8:0] file_name;
file_name = "tx_dsp.vcd"
$dumpfile(file_name);
Here, Nis the maximum number of characters in your string.
Please also take a look at Section 11.10 in the LRM. This section describes operations on string literals.

String indexing in MATLAB: single vs. double quote

I have a matrix of strings such as the following:
readFiles = [
"11221", "09";
"11222", "13";
"12821", "06";
"13521", "02";
"13522", "13";
"13711", "05";
"13921", "01";
"14521", ".001";
"15712", ".003"
];
These are used to access to some folders and files in an automatic way. Then what I want to do is the following (with ii being some integer):
FileName = strcat('../../Datasets/hc-1/d',readFiles(ii,1),'/d',...
readFiles(ii,1),readFiles(ii,2),'.dat');
data(ii,:) = LoadBinary(FileName, 6);
The string FileName is then generated using double quotes (I'm not sure why). So its value is:
FileName =
"../../Datasets/hc-1/d13921/d1392101.dat"
The function LoadBinary() returns an error when trying to perform the following operation:
lastdot = strfind(FileName,'.');
FileBase = FileName(1:lastdot(end)-1); % This line
However, if I create the string FileName manually using single quotes, the function works okay.
In a nutshell, if I try to index a string (FileName(1:lastdot(end)-1)) that is created with the lines above (leading to FileName = "../../Datasets/hc-1/d13921/d1392101.dat"), MATLAB returns an error. If I create it manually with single quotes (FileName = '../../Datasets/hc-1/d13921/d1392101.dat'), the function works right.
Why does this happen? Is there a way to fix it (i.e. convert the double-quoted string into a single-quoted one)?
Double quotes are String array, while Single one are Char array. You can convert your string array to a char one using the function char.
So you'd write :
CharFileName = char(FileName)
And it should resolve your issue.

How to Add a String inside of another string without erasing the rest of it

I'm having bad times trying to add a string inside of another string in a especific position. Every time I use this code, the rest of the string become blank.
double temperature = 22.1;
unsigned char pacote[16] = "#0123456789ABCDEF";
unsigned char temp_local[3];
dtostrf(temperatura, 3, 1, temp_local);
sprintf(pacote+3,"%s", temp_local);
or
sprintf(pacote+3,temp_local);
got the same printf:
#0122.1
istead of:
#0122.16789ABCDEF
Why is it erasing all the rest of the string and not just replace the next 4 positions after [3] and leaving the rest alone.
I'm using arduino but I think that's generic C question.
Thank you very much!!!
You should move the string data into the designated position (instead of copying the string, which includes a terminating '\0'):
memcpy(pacote+3, temp_local, strlen(temp_local));
if you are using C++ then use string::substr as in
new_string = old_string.substr(0,3) + "some text" + old_string.substr(3);
In pure C you first need to ensure that the destination has enough space. sprintf returns the number of characters written. Use this to ensure that destination has sufficient space. Then concatenate the two substrings with the new string as desired.

what is string strName<>?

I have seen code like this:
struct failed_login_res {
string errorMsg<>;
unsigned int error;
};
What does the <> at the end mean? How is it different from normal declaration like string errorMsg?
Correction: this is for RPC stub, not C++ and I can confirm that it does compile. The question is then still valid.
From a quick googling, I came across this PDF.
Section 6.9 is as follows:
Strings: C has no built-in string type, but instead uses the null-terminated “char *” convention. In XDR language, strings are declared using the “string” keyword, and compiled into “char *”s in the output header file. The maximum size contained in the angle brackets specifies the maximum number of characters allowed in the strings (not counting the NULL character). The maximum size may be left off, indicating a string of arbitrary length.
Examples:
string name<32>; --> char *name;
string longname<>; --> char *longname;

Resources