Read a txt file by URL link in MATLAB - matlab

I have a txt file in the following URL path:
https://www.dropbox.com/s/zz9qp06fykz4otw/Counter.txt
I wana read it into MATLAB. This file contains only an integer value. I used fopen() function, but it gives me error:
The file name contains characters that are not contained in the filesystem encoding. Certain operations may not work as expected.
Then, I used webread() and urlread(), but a big file is generating and not working as expected.
Is there any suggestion to how to read a simple value from an URL link?

It is because the url https://www.dropbox.com/s/zz9qp06fykz4otw/Counter.txt refers to the dropbox page and not the file. If you add ?dl=1 at the end of the link, then it will refer to the file instead. e.g.
data = urlread('https://www.dropbox.com/s/zz9qp06fykz4otw/Counter.txt?dl=1')
data =
'100'

Related

How to open a temporary file in a browser with Lua?

I'm currently working on a plugin for NeoVim in which I parse HTML from the current buffer and write it on a temporary file. Now, I want to open the temporary file in the browser, so it would show the result of the parsed HTML, but I can't get it done.
I have tried using os.execute and finding the path of the file, but it doesn't work.
Currently, my function looks like this. buffer_to_string() returns a string which contains all the content of the current buffer and matching_html() returns the matches between the content and a HTML pattern
function M.preview_html()
local content = buffer_to_string()
local matching_html = get_html(content)
local tmp_file = io.tmpfile()
tmp_file:write(matching_html)
end

Matlab fopen error with csv file

I'm just trying to import a csv file into Matlab using the textscan function. But everytime i run the program it always throws back this error
Error using textscan
Invalid file identifier. Use fopen to generate a
valid file identifier.
But as you can see in the code below i'm using fopen to prepare the file for the use of textscan.
S = 'Proto2.csv';
fidi = fopen(S);
C = textscan(fidi, '%f%s%f%f%f%f%f%f%s%f%f%f%f%f%f%f%f%f%f%f%f%f%f', 'Delimiter','\n', 'HeaderLines',11, 'CollectOutput',1);
Afterwards i'm using C to get access to data i need out of the csv file
Normally, before proceeding with reading and/or writing operations on a file handle, you must make sure that the handle returned by the fopen function is valid. From the official documentation of Matlab:
fileID = fopen(filename) opens the file, filename, for binary read
access, and returns an integer file identifier equal to or greater
than 3. MATLABĀ® reserves file identifiers 0, 1, and 2 for standard
input, standard output (the screen), and standard error, respectively.
If fopen cannot open the file, then fileID is -1.
Check your fidi variable value before proceeding with textscan, I'm pretty sure it is equal to -1. This happens either because the file is not found (if you don't specify a full absolute path, Matlab searches for it within the current working directory) or because the file has a sharing lock on it (hence, make sure that it's not being used by other applications while you attempt to read it).

ReadTable in Matlab

When I use the readtable function I get the following error:
IVcellData = readtable('RiskModelData','Sheet',2,'Range','A1:A49')
Error using readtable (line 129) Invalid parameter name: Sheet.
Would appreciate if anyone could help me.
Have you renamed Sheet 2 to something else, e.g. Datafile? If so, you need to use this name (inside single quotes) not the sheet number instead of 2 in that call.
Also, you need to make a call to
opts = detectImportOptions(yourfilename)
before the call to readtable. I suspect this one is this cause as it is not recognising Sheet as a variable.
Took me a while to discover that lot, mostly empirical as the documentation is not clear on that point.
Keith
Looks like you need to define extension:
T = readtable(filename) creates a table by reading column oriented data from a file.
readtable determines the file format from the file name's extension:
.txt, .dat, or .csv for delimited text files
.xls, .xlsb, .xlsm, .xlsx, .xltm, .xltx, or .ods for spreadsheet files
try ReadModelData.xls or .xlsx

How do I export SAS text string to a Word document using DDE?

I would like to export a string character from SAS to a word document (.docx) using Dynamic Data Exchange (DDE). Is this possible?
The SAS documentation on this is old and suggests I use the following commands:
filename testit dde 'winword|"file_path"!bookmark' notab;
data _null_;
file testit;
put 'insertstuff';
run;
SAS returns an error message:
ERROR: Physical file does not exist
Works for me.
filename testit dde 'winword|"e:\blah.docx"!bookmark' notab;
data null;
file testit;
put 'insertstuff';
run;
Steps:
Create a word document and save it in the specified path.
In word document, create a bookmark by going to Insert->Bookmark, give it a name of 'Bookmark' and press Add
Make sure both word and SAS are open. And that the document is open in word.
Run the SAS code.
Super late to the party, but there are a few issues that could cause that error:
You don't need "" around the filename, should just be:
filename testit dde 'winword|file_path!bookmark' notab;
data _null_;
file testit;
put '[Insert "stuff"]';
run;
The file path may be spelled incorrectly
You may not have permissions to the filepath. This is likely if it is a work machine.
To check:
Navigate to the filepath in the file explorer
Right click on the file
Open properties
Look for your username, click on it, and it will show you what permission you have
You have a missing/incorrect file extension (ie .doc not .docx ect)
Hope you were able to figure this out at the time :P

Extracting file names from an online data server in Matlab

I am trying to write a script that will allow me to download numerous (1000s) of data files from a data server (e.g, http://hydro1.sci.gsfc.nasa.gov/thredds/catalog/GLDAS_NOAH10SUBP_3H/2011/345/). Unfortunately, the names of the files in each directory are not formatted in a similar way (the time that they were created were appended to the end of the file name). I need to be able to specify the file name to subset the data (I have a special tool for these data types) and download it. I cannot find a function in matlab that will extract the file names.
I have looked at URLREAD, but it downloads everything including html code.
Thanks for your help!
You can easily parse the link.
x=urlread(url)
links=regexp(x,'<a href=''([^>]+)''>','tokens')
Reads every link, you have to filter all unwanted links.
For example this gets all grb files:
a=regexp(x,'<a href=''([^>]+.grb)''>','tokens')