How can I create a new variable with values from text files, without actually loading all of them?
For example, I have two text files: the first one contains a**bc**d at line 18. The second one contains a**cf**d at the same position. The result I’m looking for is:
bc
cf
Unfortunately, your question is poorly worded, but the example provided me with clues of what you want to achieve. I hope the following helps:
Skipping lines
To skip lines, you can effectively use
fgetlorfgetsand discard its result. This simply advances the file pointer to the line of your desire. Regarding your simplified example, if you want to skip the first 17 lines and read the 18th, you can do as follows:Or read each line into the same variable (
sin my example), overriding its previous value:An even shorter alternative is to this is to employ
textscanwith the'HeaderLines'option:Extracting a substring from a string
Since a string is an array (vector) of characters, you can use vector indexing to extract any elements you want. For example, to extract positions 2 and 3 from
s = 'abcd'to obtainbc, you can use the expressions([2 3]), like so: