MATLAB MATLAB String Operations and File I/O 1 — Questions and Answers
Question 1: Which MATLAB function concatenates two strings?
- strcat (Correct answer)
- concat
- append
- join
Correct answer: strcat
strcat concatenates character arrays or string arrays, removing trailing whitespace from character array inputs.
Question 2: How do you convert a number to a string in MATLAB?
- num2str(x) (Correct answer)
- str(x)
- toString(x)
- convert(x, 'str')
Correct answer: num2str(x)
num2str converts a numeric value to a character array representation of that number.
Question 3: What function reads a text file line by line in MATLAB?
- fgetl (Correct answer)
- fread
- fscanf
- fgets
Correct answer: fgetl
fgetl reads one line from a file and removes the newline character, returning -1 at end of file.
Question 4: Which MATLAB function searches for a pattern in a string and returns its position?
- strfind (Correct answer)
- find
- regexp
- strsearch
Correct answer: strfind
strfind returns the starting indices of all occurrences of a pattern within a string.
Question 5: What does `textscan` do in MATLAB?
- Reads formatted data from a string or file into a cell array (Correct answer)
- Scans text for spelling errors
- Prints formatted text to screen
- Converts text to numeric arrays
Correct answer: Reads formatted data from a string or file into a cell array
textscan reads formatted data from a string or file, parsing it into a cell array according to format specifiers.
Question 6: Which function in MATLAB reads a CSV file into a table?
- readtable (Correct answer)
- csvread
- importdata
- textread
Correct answer: readtable
readtable reads a CSV or other delimited text file and returns a MATLAB table object with column headers.
Which MATLAB function concatenates two strings?